3

How to define the value for controller as elements inside custom directive link function?

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
  • 2
    the javascript doesn't seem to be correct near the link-function. Is this a typo in the question or is this how your code looks? one ) to much and the ; should belong to the return statement Commented Oct 16, 2015 at 6:49
  • yes. its typo mistake. link:function(scope,elements,ngModelCtrl){ //How to access ng-diasbled value here }; Commented Oct 16, 2015 at 7:01

2 Answers 2

1

Inside link function write as scope.ctrl.disable to access the disable value.

Sign up to request clarification or add additional context in comments.

Comments

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

7 Comments

When you set more than one required controller - they are passed as array into Link function. Order of this array is the same as in "require"
If we need to use ngModelCtrl,$parsers then it should be var ngModelCtrl = ctrls[0]; right?
I am getting following error. Error: [$compile:ctreq] errors.angularjs.org/1.3.18/$compile/…
the controller(s) are the 4th parameter to the link function. the third one is attributes.
yes. link: function (scope, element, attrs, ctrls)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.