让我们想一想这里最简单的例子。
如何从指令A的模板中发送文本输入的值作为指令B模板中以ng-repeat形式存在的列表的过滤器?
答案 0 :(得分:0)
<强> HTML 强>
<first-directive></first-directive>
<second-directive></second-directive>
<强> JS 强>
app.controller('myCtrl',function($scope, $location, $timeout) {
$scope.items=['a ab', 'asd', 'www', '123'];
}
);
app.directive('firstDirective', function() {
return {
restrict: 'EA',
scope: false,
template: '<div><input ng-model="input"></div>'
};
});
app.directive('secondDirective', function() {
return {
restrict: 'EA',
scope: false,
template: '<div ng-repeat="item in items | filter:input">{{ item }}</div>'
};
});
<强> HTML 强>
<first-directive filter-string="filterString"></first-directive>
<second-directive filter-string="filterString" items="items"></second-directive>
<强> JS 强>
app.controller('myCtrl',function($scope, $location, $timeout) {
$scope.items=['a ab', 'asd', 'www', '123'];
$scope.filterString = '';
}
);
app.directive('firstDirective', function() {
return {
restrict: 'EA',
scope: { filterString:"=" },
template: '<div><input ng-model="filterString"></div>'
};
});
app.directive('secondDirective', function() {
return {
restrict: 'EA',
scope: { filterString:"=",
items:"="},
template: '<div ng-repeat="item in items | filter:filterString">{{ item }}</div>'
};
});