以下の関数は、ルートスコープ内の変数を定義します。ディレクティブスコープの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'];
以下ディレクティブに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
}
});
しかし上記のコードdoesntの仕事。これは、ディレクティブスコープ内で 'buttons' varを直接定義すると機能します。
ジムのおかげ。 1つの質問はなぜ私の属性にbuttons = "buttons"を指定する必要があるかです。また、それは何をしますか?なぜ、上記の属性宣言なしでコードが動作しないのでしょうか? – murtaza52
ディレクティブコントローラがscope、element、attribute、transcludeを受け入れるため、ur第3パラメータは実際には属性パラメータである$ rootScopeです。ここでは、UUコントローラコード: 関数($スコープ、$要素、$ rootScope)です。 文書を参照してくださいhttp://docs.angularjs.org/guide/directive – AsadYarKhan
@ AsadYarKhan私はリンクとコンパイルとコントローラを混乱させていると思います。 – Usagi