我有一個指令可以創建一個輸入字段。 我需要將此輸入字段的ng-model屬性設置爲$ rootScope 變量的值。 這是因爲我希望輸入字段位於佈局中,並根據加載的頁面綁定到不同的模型。 我想我會在每個控制器中設置這個全局變量並在指令中訪問它。
ATM變量是硬編碼
App.run(function($rootScope){
$rootScope.mymodel = 'search.name';
})
而且指令
Directives.directive('inputFilter', function(){
return{
restrict: 'E',
replace:true,
controller: function($scope, $rootScope){
console.log($scope.mymodel);
console.log($rootScope.mymodel)
},
template: '<input class="filter" type="text" ng-model="mymodel" placeholder="Nach filtern">'
}
});
它被呈現爲
<input class="filter ng-pristine ng-valid" type="text" ng-model="mymodel" placeholder="Filter">
和輸入字段中的文本是爲MyModel的價值變量。 console.log顯示
search.name
search.name
任何人都可以請解釋一下這個問題?
也可以看看在你的模板,持有該有自己的範圍指令到$ rootScope參考引用$根。請參閱 - http://stackoverflow.com/questions/22216441/what-is-the-difference-between-scope-root-and-rootscope – cchamberlain 2015-03-29 01:49:17