简体   繁体   English

角访问子component()v1.5中的方法

[英]Angular access a method inside a child component() v1.5

I'm using angular v1.5 and changing from directives to component() and I'm facing a situation 我正在使用angular v1.5,并从指令更改为component(),我正面临一种情况

I have this component hierarchy grandfather > father > grandson and I'm trying to make a method available inside grandson component() so I'm passing the method from grandfather through grandson but this doesn't work, someone can give me a clue? 我有这个组件层次结构,“祖父”>“父亲”>“孙子”,并且我试图在孙子component()中提供一种方法,所以我将方法从祖父传递给孙子,但这是行不通的,有人可以给我一个提示吗? maybe show me what I'm doing wrong? 也许告诉我我在做什么错? :/ :/

I put a bit of code here to explain what I'm doing: https://gist.github.com/WagnerMoreira/29ca7764eaa335784e06eccf8b0223cf 我在这里放置了一些代码来解释我在做什么: https : //gist.github.com/WagnerMoreira/29ca7764eaa335784e06eccf8b0223cf

Typically it can be done using require and, optional, $onInit like so: 通常,它可以使用require和可选的$onInit来完成, $onInit所示:

require: {
    parent: '^nameOfParent'
}

Then in the controller that is making use of the inherited function you can initialize it like this: 然后,在使用继承函数的控制器中,您可以像这样初始化它:

vm.$onInit = init();

function init() {
    vm.foo = vm.parent.foo();
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM