如何在指令中应用filter

时间:2017-02-09 16:05:27

标签: angularjs

我有一个指令:

(function () {
    angular.module('app.components')
        .directive('myDirective', myDirective);

    myDirective.$inject = ['$http', '$timeout'];

    function myDirective($http, $timeout) {
        return {
            restrict: 'E',
            scope: {

            },
            link: function (scope, el, attrs) {
               //MyCode
        },
            templateUrl: 'js/myDirective/myDirective.html'
        };
    }

现在,我想申请I过滤器:

.filter('filterByProperty', function () {
    /* array is first argument, each additional argument is prefixed by a ":" in filter markup*/
    return function (dataArray, searchTerm, propertyName) {
        if (!dataArray) return;
        /* when term is cleared, return full array*/
        if (!searchTerm) {
            return dataArray
        } else {
            /* otherwise filter the array */
            var term = searchTerm.toLowerCase();
            return dataArray.filter(function (item) {
                return item[propertyName].toLowerCase().indexOf(term) > -1;
            });
        }
    }
});

我的问题是我不明白我应该把这段代码放在哪里。因为无论我把它放在哪里,它都会出现如下错误: - Uncaught SyntaxError: Unexpected token。谁能告诉我什么是正确的语法?

0 个答案:

没有答案