在angularjs中添加过滤器

时间:2015-06-17 13:46:47

标签: angularjs angularjs-filter

我是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

1 个答案:

答案 0 :(得分:1)

在您的应用中,您必须为您的应用使用该模块。

这样做,它应该工作

angular.module('myApp', ['filters.stringUtils'])