anglejs – 角度ng重复表达式作为变量
发布时间:2020-12-17 07:49:07 所属栏目:安全 来源:网络整理
导读:我想做这样的事情: ul li ng-repeat="{{myRepeatExpression}}"{{row.name}}/li/ul 但是因为ng-repeat逻辑处于指令的编译状态,它将{{myRepeatExpression}}视为普通字符串而不是变量.哪个不行,显然. 有没有解决办法? 您只能使用ng表示,而不是内插值. 现在为
我想做这样的事情:
<ul> <li ng-repeat="{{myRepeatExpression}}">{{row.name}}</li> </ul> 但是因为ng-repeat逻辑处于指令的编译状态,它将{{myRepeatExpression}}视为普通字符串而不是变量.哪个不行,显然. 有没有解决办法?
您只能使用ng表示,而不是内插值.
现在为了创建一个动态可重复列表,您可以尝试: >使用在ng-repeat中动态返回列表的函数 – 这可能更昂贵,因为角度需要首先调用函数,然后确定在执行$digest循环时集合是否已更改 Demo plunker JS: app.controller('MainCtrl',function($scope) { var values1 = [{name:'First'},{name:'Second'}]; var values2 = [{name:'Third'},{name:'Fourth'},{name:'Fifth'}]; //1. function way $scope.getValues = function(id) { if(id === 1) { return values1; } if(id === 2) { return values2; } } //2. watch way $scope.values = undefined; $scope.$watch('id',function(newVal) { $scope.values = $scope.getValues(newVal); }); }); HTML: <!-- Here we pass the required value directly to the function --> <!-- this is not mandatory as you can use other scope variables and/or private variables --> <ul> <li ng-repeat="v in getValues(id)">{{v.name}}</li> </ul> <!-- Nothing special here,plain old ng-repeat --> <ul> <li ng-repeat="v in values">{{v.name}}</li> </ul> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容