Javascript 如何在AngularJS中正确使用指令
我正在用AngularJS的指令做我的第一步,而这个指令对我不起作用 我有一个Javascript 如何在AngularJS中正确使用指令,javascript,angularjs,Javascript,Angularjs,我正在用AngularJS的指令做我的第一步,而这个指令对我不起作用 我有一个ng repeat列表 <ul ng-after-list-filter> <li ng-repeat="item in items | filter:activeFilter.filterType = activeFilter.filterValue">...</li> </ul> 如何使其工作?我假设scope.activeFilter.filterValu
ng repeat
列表
<ul ng-after-list-filter>
<li ng-repeat="item in items | filter:activeFilter.filterType = activeFilter.filterValue">...</li>
</ul>
$('ul').html()
?我试过了,但它输出了一个错误,我将原始文本放入javascript似乎要重用筛选器中的筛选值?对吗 如果是这样的话,您真正需要做的就是:
<!-- initial filter -->
<ul ng-after-list-filter>
<li ng-repeat="item in filteredData = (items | filter:activeFilter.filterType)">{{item}}</li>
</ul>
<!-- reuse what you filtered -->
<div ng-repeat="item in filteredData">{{item}}</div>
$last可用于确定创建最后一个
directive('checkLast', function () {
return function (scope, element, attrs) {
if (scope.$last === true) {
element.ready(function () {
// manipulate ul here, or if that doesn't work
// try with $timeout:
// $timeout(function() {
// do manip here
//}, 0, false); // remove false if you need $apply to run
另见
我不确定element.ready()是否必要,或者它是否适用于动态生成的元素。我不明白为什么要复制列表?现在我有两个列表而不是一个,我仍然不知道当列表完成更改后如何触发函数。哦,对不起,我想我不明白你的问题。如果需要知道列表何时被过滤,可以向上面的
filteredData
参数添加$watch;希望这是个打字错误,你的意思是==?
myModule.directive('ngAfterListFilter',function(){
return {
restrict: 'A',
link: function(scope,element,attrs) {
scope.$watch('filteredData', function(val) {
console.log('do something here');
});
}
}
});
directive('checkLast', function () {
return function (scope, element, attrs) {
if (scope.$last === true) {
element.ready(function () {
// manipulate ul here, or if that doesn't work
// try with $timeout:
// $timeout(function() {
// do manip here
//}, 0, false); // remove false if you need $apply to run