【发布时间】:2015-08-11 03:52:28
【问题描述】:
我正在尝试使用指令作为我的数据的过滤器。这是我的数据数组:
$scope.data = [{
"id": 1,
"name": "een"
}, {
"id": 2,
"name": "twee"
}];
这是我作为选择实现的指令过滤器:
.directive('myFilter', function() {
return {
restrict: 'E',
templateUrl: 'my-dialog.html',
link: function($scope, $element, $attributes) {
var testing = $scope.data | {
filter: $element.choice
};
console.log($element.choice);
console.log($attributes.choice);
console.log($scope.choice);
console.log(testing);
}
};
});
和模板网址:
<select name="" id="" ng-model='choice'>
<option value=1>1</option>
<option value=2>2</option>
</select>
这是定义指令的html:
<my-filter dt="data"></my-filter>
plunkr:http://plnkr.co/edit/zqS5HyH3AqaMVtEv107p?p=preview
如何更改指令,以便当我在组合中选择 1 时,数据被过滤为仅 1 记录 id=1 的记录?
更新:当我更改组合上的值时,我稍微更改了代码以尝试过滤 $scope.data:
link: function($scope) {
$scope.$watch('choice', function() {
var testing = $filter('filter')($scope.data,$scope.choice);
console.log(testing);
});
}
【问题讨论】:
标签: angularjs angularjs-directive