Search code examples
angularjsangularjs-components

Angular 1.5 : Access parent component without knowing the name of the parent


I am working on 2 components using Angular 1.5.

I want to access the parent from the child, and I managed to do it using :

require: {
    parent: '^parentCmp'
}

The thing is, I would like to do the same, but without "fixing" the parent as "parentCmp", since it won't always be this component I'll have as a parent.

Thanks.


Solution

  • You should not do it :) But if you have to, you can pass it as regular binding. Child component:

    bindings: {
        parent: '<'
    }
    

    And in parent template (I presume that you use $ctrl alias for controller, if it's not true use name you provide):

    <child-component
        parent="$ctrl"
    >
    </child-component>