如何在指令模板Angular JS中使用ng-repeat

时间:2015-06-24 09:56:35

标签: angularjs ng-repeat

我在显示控制器传递给指令模板的JSON对象数据时遇到问题。这是我的代码。

指令

app.directive('heroes', function(){
    return{
        scope:{
            heroes: '='
        },
        template: '<li ng-repeat="x in hereos">{{ x.Name }} </li>', // DOESNT WORK
        link:function(scope,element,attributes){

            });
        }
    }
});

控制器

app.controller('MainController',function($scope, $http){
    $scope.getData = function(){
          $http({
              url: 'js/directives/herolist.php',
              method: "GET"
        }).success(function (data) { $scope.heroes = data.records; })

    }

1 个答案:

答案 0 :(得分:3)

Working Plunkr

您应该在HTML中包含该指令并稍微重命名您的值

html可以是

 <heroes data="heroes"></heroes>

然后在你的指令中你会做

 scope:{
        heroes: '=data' 
    }

如果你愿意做英雄:&#34; =&#34;而且你并没有限制指令让我们说一个元素,那么你基本上都要包括指令两次(你不想要那个)。如果你确实想使用英雄作为属性,就像这样

<heroes heroes="heroes"></heroes>

然后添加

  

限制:&#34; E&#34;

指示你的指示。