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

AngularJS,在ng repeat中使用指令中的范围,angularjs,resources,scope,directive,Angularjs,Resources,Scope,Directive,我想在指令中使用$resource,将结果绑定到范围,然后在指令本身的模板中使用它。像这样: .directive('myCategory', ['$compile', 'webService', function($compile, webService) { return { restrict: 'E', link: function(scope, elm, attrs) { scope.categories = webSer

我想在指令中使用$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>'

    };
}]);;
.directive('myCategory',['$compile','webService',function($compile,webService){
返回{
限制:'E',
链接:功能(范围、elm、属性){
scope.categories=webService.query({resourceName:'category'});
},

模板:“您可能应该在控制器中访问数据,而不是在指令中。请尝试此操作

.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>'

          };
}]);;

如果可以,请确保您的控制器封装了您的指令。

啊,没关系,它只是一个输入错误模板:“
  • ”我忘记了在我的ng repeat中的categories后面加上双引号(“)
    .controller('myCtrl', function($scope, webService) {
    
    $scope.categories =  webService.query({resourceName:'category'});
    
    }