我想做这样的事情:
- <ul>
- <li ng-repeat="{{myRepeatExpression}}">{{row.name}}</li>
- </ul>
但是因为ng-repeat逻辑处于指令的编译状态,它将{{myRepeatExpression}}视为普通字符串而不是变量.哪个不行,显然.
有没有解决办法?
您只能使用ng表示,而不是内插值.
现在为了创建一个动态可重复列表,您可以尝试:
现在为了创建一个动态可重复列表,您可以尝试:
>使用在ng-repeat中动态返回列表的函数 – 这可能更昂贵,因为角度需要首先调用函数,然后确定在执行$digest循环时集合是否已更改
> $对于触发更改列表的范围的特定变量,可以看到更有效率,但是如果您的动态列表依赖于多个变量,它可能会变得更冗长,并可能导致潜在的错误,从忘记添加新的$在需要新变量时观察
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>