在 angularjs 中添加过滤器
Adding a filter in angularjs
我是 angularjs 的新手,我正在尝试创建一个过滤器来删除字符串中的空格。
removeSpaces.js
angular.module('filters.stringUtils', [])
.filter('removeSpaces', [function() {
return function(string) {
if (!angular.isString(string)) {
return string;
}
return string.replace(/[\s]/g, '');
};
}])
home.html
<div ng-controller="ItemController">
<p ng-repeat="item in items">
<a href="/items/{{ item.item_name | removeSpaces }}">{{ item.item_name }}</a>
</p>
itemcontroller.js
angular.module('myApp').controller('ItemController', [
'$scope', 'Services', '$http','removeSpaces', function($scope, Services, $http,removeSpaces) {
$http.defaults.headers.common['Accept'] = 'application/json';
return $scope.services = Services.query();
}
]);
我收到这个错误:
Unknown provider: removeSpacesFilterProvider <- removeSpacesFilter
在您的应用中,您必须为您的应用使用该模块。
所以这样做,它应该工作
angular.module('myApp', ['filters.stringUtils'])
我是 angularjs 的新手,我正在尝试创建一个过滤器来删除字符串中的空格。
removeSpaces.js
angular.module('filters.stringUtils', [])
.filter('removeSpaces', [function() {
return function(string) {
if (!angular.isString(string)) {
return string;
}
return string.replace(/[\s]/g, '');
};
}])
home.html
<div ng-controller="ItemController">
<p ng-repeat="item in items">
<a href="/items/{{ item.item_name | removeSpaces }}">{{ item.item_name }}</a>
</p>
itemcontroller.js
angular.module('myApp').controller('ItemController', [
'$scope', 'Services', '$http','removeSpaces', function($scope, Services, $http,removeSpaces) {
$http.defaults.headers.common['Accept'] = 'application/json';
return $scope.services = Services.query();
}
]);
我收到这个错误:
Unknown provider: removeSpacesFilterProvider <- removeSpacesFilter
在您的应用中,您必须为您的应用使用该模块。
所以这样做,它应该工作
angular.module('myApp', ['filters.stringUtils'])