gla*_*ill 12 angularjs angularjs-directive angularjs-scope
我有一个创建输入字段的指令.我需要将此输入字段的ng-model属性设置为$ rootScope变量的值.这背后的原因是我希望输入字段在布局中,并根据加载的页面绑定到不同的模型.我以为我会在每个控制器中设置这个全局变量并在指令中访问它.
ATM变量是硬编码的
App.run(function($rootScope){
$rootScope.mymodel = 'search.name';
})
Run Code Online (Sandbox Code Playgroud)
和指令
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">'
}
});
Run Code Online (Sandbox Code Playgroud)
它被渲染为
<input class="filter ng-pristine ng-valid" type="text" ng-model="mymodel" placeholder="Filter">
Run Code Online (Sandbox Code Playgroud)
输入字段内的文本是mymodel变量的值.console.log显示
search.name
search.name
Run Code Online (Sandbox Code Playgroud)
有谁能请对这个问题有所了解?
Mar*_*cok 13
我认为你想要的是
template: '<input class="filter" type="text" ng-model="'
+ $rootScope.mymodel + '" placeholder="Nach filtern">'
Run Code Online (Sandbox Code Playgroud)
小提琴.
请注意,您需要注入$rootScope您的指令:
Directives.directive('inputFilter', function($rootScope) {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45516 次 |
| 最近记录: |