AngularJS评估指令模板$ rootScope变量 [英] AngularJS evaluate $rootScope variable in directive template

查看:158
本文介绍了AngularJS评估指令模板$ rootScope变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建一个输入域指令。
我需要此输入字段的NG-模型属性设置为$ rootScope的价值
变量。
这背后的原因是,我想输入字段是在布局,并结合具体取决于加载的页面不同的型号。
我想我会在每个控制器设置这个全局变量和访问它的指令。

I have a directive that creates an input field. I need to set the ng-model attribute of this input field to the value of a $rootScope variable. The reason behind this is that I want the input field to be in the layout, and bind to different models depending on what page is loaded. I thought I'd set this global variable in each controller and access it in the Directive.

ATM变量是很难codeD

ATM the variable is hard coded

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

and the text inside the input field is the value of mymodel variable. The console.log shows

search.name
search.name  

任何人都可以请流下在这个问题上的一些灯?

Could anyone please shed some light on this issue?

推荐答案

我想什么你想要的是

template: '<input class="filter" type="text" ng-model="' 
  + $rootScope.mymodel + '" placeholder="Nach filtern">'

小提琴

请注意,你将需要注入$ rootScope到你的指令:

Note that you will need to inject $rootScope into your directive:

Directives.directive('inputFilter', function($rootScope) {

这篇关于AngularJS评估指令模板$ rootScope变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆