0

Say I have the following two directives that work with each other

directive('parent', function() {
    return {
        scope: {},
        require: 'ngModel',
        controller: function($scope) {
            this.doSomething = function() {
                //How to use ngModelController here?
            };
        },
        link: function(scope, element, attr, ngModelController) {
        }
    };
});

directive('child', function() {
    return {
        scope: {},
        require: '^parent',
        link: function(scope, element, attr, parent) {
            parent.doSomething();
        }
    };
});

How can I use the ngModelController from within the controller of parent? I suppose I could do scope.ngModelController = ngModelController inside the link function but that seems hacky. Is there a better solution?

3
  • I guess you could put it in the scope in the link function? Does seem slightly hacky, but can't think of anything else... Commented Mar 18, 2014 at 21:23
  • Try require: '?ngModel'. That should bubble up through parents looking for the controller. Commented Mar 18, 2014 at 22:23
  • Apparently this was a duplicate! stackoverflow.com/questions/21231294/… Commented Mar 31, 2014 at 23:35

1 Answer 1

0

Is this any less hacky?

directive('parent', function() {
    var ngModelCtrl = null;
    return {
        scope: {},
        require: 'ngModel',
        controller: function($scope) {
            this.doSomething = function() {
                //How to use ngModelController here?
                if (ngModelCtrl) {
                }
            };
        },
        link: function(scope, element, attr, ngModelController) {
            ngModelCtrl = ngModelController;
        }
    };
});
Sign up to request clarification or add additional context in comments.

Comments

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.