我正在寻找一种将过滤后的数组传递给指令的方法:
我尝试过以下方法:
<my-directive model="myArray | filter:{myProperty: 'some value' }" />
但这不起作用。我认为它应该与 ng-repeat 一起使用,因为在这里我只是传递一个函数而不是过滤后的数组。
除了制作数组的过滤副本之外,还有其他方法可以做到这一点吗?
EDIT
这是完整的代码:
<request-service type="editing" jobs="vm.selectedMaterial.jobs | filter:{service.code: 'ED'}"></request-service>
<request-service type="translation" jobs="vm.selectedMaterial.jobs | filter:{service.code: 'TR'}"></request-service>
和指令:
(function () {
'use strict';
var directiveId = 'requestService';
angular.module('comp.domain.directives').directive(directiveId, [directiveFunc]);
function directiveFunc(dependency) {
return {
restrict: 'E',
templateUrl: 'app/dm/views/templates/requestService.html',
scope: {
type: '@',
jobs: '='
},
link: function (scope, element, attrs) {
}
};
}
})();
这样做时,我收到错误“将循环结构转换为 JSON”
EDIT 2
按照建议的解决方案,我这样做了:
$scope.filterJob = function (type) {
if ($scope.vm.selectedMaterial) {
return $scope.vm.selectedMaterial.jobs.filter(function (job) { return job.service.code === type; });
};
}
并在视图中:
<request-service type="ED" jobs="filterJob('ED')"></request-service>
但这仍然给我同样的错误。