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'];
下面的指令中的html取决于rootscope中的变量 –
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 } });
但是上面的代码不行.如果我直接在指令范围中定义了“buttons”var,它将起作用.