角度ng重复和动态属性

时间:2015-10-14 10:14:35

标签: angularjs angularjs-ng-repeat

我有一个php对象,我通过函数传递给ng。这个对象有几个属性,例如:paragraphe1,paragraphe2,paragraphe3,最多8个。

我尝试使用ng-repeat循环显示它,如:

<div ng-repeat="i in [1,2,3,4,5,6,7,8]">
Paragraphe {{i}} >> {{paragraphe+i}}
</div>

它显示:

  • Paragraphe 1&gt; 1
  • Paragraphe 2&gt; 2
  • ...
  • Paragraphe 8&gt; 8

而不是paragraphe1的内容,...... 8

感谢您的帮助。 FX

2 个答案:

答案 0 :(得分:1)

假设你的段落定义是:

$scope.paragraph = {
    paragraph1: 'lorem ipsum 1',
    paragraph2: 'lorem ipsum 2',
    paragraph3: 'lorem ipsum 3',
    // ...
}

模板应如下所示:

<div ng-repeat="i in [1,2,3,4,5,6,7,8]">
    Paragraphe {{i}} >> {{paragraph['paragraph' + i]}}
</div>

参见 js fiddle

答案 1 :(得分:1)

不是传递硬编码的值,而是可以使用ng-repeat

中的键值对

查看:

<div ng-app="myApp" ng-controller="demoCtrl">
  <div ng-repeat="(key, value) in paragraph">
    {{key}} >> {{value}}
  </div>
</div>

<强> JS:

// module
var app = angular.module('myApp', []);

// filter
app.filter('dayFilter', function() {
  return function(input) {
    var filterFunction = function (item) {
        return item.days >= 1 && item.days <= 7;
    };
    return input.filter(filterFunction);
  };
});

//controller
app.controller('demoCtrl', function ($scope) {
  $scope.paragraph = {
    paragraph1: 'lorem ipsum 1',
    paragraph2: 'lorem ipsum 2',
    paragraph3: 'lorem ipsum 3'
  }
});