相关疑难解决方法(0)

无法在指令范围内访问rootscope var

下面的函数定义了根镜中的变量.

function MyCtrl($scope, $rootScope) {
  $rootScope.buttons = [{href: '#/students', icon:'icon-ok'},
                         {href: '#/students', icon:'icon-remove'},
                         {href: '#/students/new', icon:'icon-plus'}];
}
MyCtrl.$inject = ['$scope', '$rootScope'];
Run Code Online (Sandbox Code Playgroud)

下面的指令中的html取决于rootcope中的变量 -

angular.module('btnbar.directive', []).
directive("btnBar", function(){
  return {
    restrict: 'E',
    scope :{},
    controller: function($scope, $element,$rootScope) {
    },
    template:'<div class="btn-toolbar">' +
      '<a class="btn" ng-repeat="b in buttons" href={{b.href}}>' +
      '<i class={{b.icon}}></i></a></div>',
    replace:true
  }
});
Run Code Online (Sandbox Code Playgroud)

但是上面的代码不起作用.如果我直接在指令范围中定义'buttons'var,它就可以工作.

angularjs angularjs-scope

13
推荐指数
2
解决办法
2万
查看次数

标签 统计

angularjs ×1

angularjs-scope ×1