【发布时间】:2013-03-15 13:55:32
【问题描述】:
我有一个创建输入字段的指令。 我需要将此输入字段的 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
谁能解释一下这个问题?
【问题讨论】:
-
还可以看看在您的模板中引用 $root,它在具有自己范围的指令中包含对 $rootScope 的引用。见 - stackoverflow.com/questions/22216441/…
标签: angularjs angularjs-directive angularjs-scope