AngularJS,在ng-repeat中使用指令范围

时间:2014-04-29 19:17:47

标签: angularjs resources scope directive

我想在指令中使用$ resource,将结果绑定到作用域,然后在指令本身的模板中使用它。像这样:

.directive('myCategory', ['$compile', 'webService', function($compile, webService) {
    return {
        restrict: 'E',
        link: function(scope, elm, attrs) {
            scope.categories =  webService.query({resourceName:'category'});
        },
        template: '<li ng-repeat="category in categories><a href="#/blog/category/{{category.id}}">{{category.name}}</a></li>'

    };
}]);;

1 个答案:

答案 0 :(得分:0)

您可能应该在控制器中访问您的数据,而不是指令。试试这个

.directive('myCategory', function() {
          return {
              restrict: 'E',
      link: function(scope, elm, attrs) {

      },
      template: '<li ng-repeat="category in categories><a href="#/blog/category/{{category.id}}">{{category.name}}</a></li>'

          };
}]);;
然后控制器......

.controller('myCtrl', function($scope, webService) {

$scope.categories =  webService.query({resourceName:'category'});

}

应该有效,请确保您的控制器封装了您的指令。