Angularjs: inject required directive-controller into the controller instead of the link function

后端 未结 3 747
挽巷
挽巷 2020-12-15 22:33

Normal use cases in angular

If you have a parent directive and a child directive you create methods in the controller of the parent directive and re

3条回答
  •  生来不讨喜
    2020-12-15 23:38

    Taken from romario333's comment: The cleanest solution is to simply use

    var topController = $element.controller('top') // pass directive name or controller name
    

    From the docs:

    controller(name) - retrieves the controller of the current element or its parent. By default retrieves controller associated with the ngController directive. If name is provided as camelCase directive name, then the controller for this directive will be retrieved (e.g. 'ngModel').

    $element can be injected into your directive controller.

提交回复
热议问题