$ compile不能与ng-repeat一起使用

时间:2015-11-13 14:30:12

标签: javascript angularjs ionic-framework

我的应用中有两个显示&管理名为' Categories' 我在我的应用中为两个不同的视图使用了相同的模板。在第一个视图中,我想给出#34;编辑"用户权利&在另一方面,我不想要编辑'按钮显示。隐藏&显示'编辑'按钮由以下自定义指令处理:

angular.module('starter.directives', [])
    .directive('categoryEdit', function($ionicHistory, $compile) {
       return{
           restrict: 'A',
           priority: 1001,
           compile: function(element){
               var categoryEdit = $ionicHistory.backView() ? false : true;
               element.attr('ng-show', categoryEdit);
               var fn=$compile(element, null, 1);
               return function(scope){
                   fn(scope);
                };
            }
    }
})

整个过程一直很好,直到我在模板中包含ng-repeat。经过一些调试后,我想通过排除" terminal:true"从指令我到模板的路线工作正常。但是,编辑'仍显示在两个视图中。事实证明,$ compile不能使用ng-repeat。

这是我的模板:

<div class="list" ng-repeat="category in categories"><!--has nested ng-repeat-->
          <div class="item item-button-right assertive">
              <span>{{category.name}}</span>
              <div class="button-group">
                  <button ng-click="editCategory(category)"
                      category-edit
                      class="button button-clear button-energized button-small">Edit</button>
                  <button class="button button-clear button-energized button-small">Use</button>
              </div>
          </div>
          <div class="item" ng-repeat="item in category.items">{{item}}</div>
</div>

1 个答案:

答案 0 :(得分:0)

}

ngRepeat将克隆元素,因此如果在链接阶段执行此操作,则将范围应用于错误的元素,这是当前元素