如何访问angularJS控制器作为变量?

时间:2015-10-16 06:43:47

标签: angularjs

如何将控制器的值定义为自定义指令链接函数中的元素?

HTML

<div ng-controller="myCtrl as ctrl">
   <input type="text" ng-model=ctrl.inputvalue" my-directive/>
   <button value="submit" ng-disabled="ctrl.disable"/>
</div>

JS

app.controller('myCtrl',function(){
    var vm = this;
    vm.inputValue = 'Qwerty';
});
app.directive('myDirective',function(){
    return{
        require:'ngModel',
        link:function(scope,elements,ngModelCtrl){
        //How to access ng-diasbled value here
        });
    }    
});

2 个答案:

答案 0 :(得分:1)

app.directive('myDirective',function(){
        return{
            require:'ngModel, ^myCtrl',
            link:function(scope,elements,ctrls){
                var d = ctrls[1].disable
            });
        }    
    });

答案 1 :(得分:1)

内部链接功能写为 scope.ctrl.disable 以访问禁用值。