下面的函数定义了根镜中的变量.
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,它就可以工作.