动态变量为angularjs ng-repeat

时间:2014-10-18 22:58:45

标签: javascript angularjs angularjs-scope angularjs-ng-repeat

进入第一个循环,我恢复了一个id,我尝试定义其他循环:

    <ul ng-show="feed{{raterie.idFB}} === true" ng-init="var myFeed = myFeedFunction(raterie.idFB);">
        <li ng-repeat="feed in getFeeder(raterie.idFB) | limitTo:1">adoptions are : {{feed.title | cleanit}}</li>
        <li ng-repeat="feed in feeder{{raterie.idFB}} | limitTo:1">adoptions are : {{feed.title | cleanit}}</li>
        <li ng-repeat="feed in feeder{raterie.idFB} | limitTo:1">adoptions are : {{feed.title | cleanit}}</li>
        <li ng-repeat="feed in feeder[raterie.idFB] | limitTo:1">adoptions are : {{feed.title | cleanit}}</li>
        <li ng-repeat="feed in feeder['raterie.idFB'] | limitTo:1">adoptions are : {{feed.title | cleanit}}</li>
        <li ng-repeat="feed in myFeed | limitTo:1">adoptions are : {{feed.title | cleanit}}</li>
        <li ng-repeat="feed in feeder846097918756247 | limitTo:1">adoptions is : {{feed.title | cleanit}}</li>
    <ul>

我不明白如何制作&#39; feeder846097918756247&#39;自动构建。最后的li是我想要的,我只用一个例子手动把它放到第一个循环

我在$ scope中尝试了两种解决方案:

    // define the value for ng-repeat (1st solution)
    $scope.getFeeder = function(id) {
        var idr = 'feeder' + id;
        return idr;
    }
    // define the value for ng-repeat (2nd solution)
    function myFeedFunction(pId) {
        return eval('feeder' + pId) ;
    }

它没有用。

我的在线应用程序位于:http://www.monde-du-rat.fr/pmr/

1 个答案:

答案 0 :(得分:6)

为了得到

<li ng-repeat="feed in getFeeder(raterie.idFB) | limitTo:1">adoptions are : {{feed.title | cleanit}</li>

要工作,你需要从getFeeder函数中的scope属性返回数组。

$scope.getFeeder = function(id) {
    return $scope['feeder' + id];
}

或者更好,而不是将feeder846097918756247作为属性直接放在范围上。将相应数据放在范围内的对象中。

 $scope.feeder = {};
 $scope.feeder['846097918756247'] = someArray;
//etc...

然后做: -

<li ng-repeat="feed in feeder[raterie.idFB] | limitTo:1">adoptions are : {{feed.title | cleanit}}</li>