Javascript AngularJS组件要求:组件一或组件二
我想将模板中的一些函数传递给当前活动的父组件:Javascript AngularJS组件要求:组件一或组件二,javascript,angularjs,angularjs-components,Javascript,Angularjs,Angularjs Components,我想将模板中的一些函数传递给当前活动的父组件: <div ng-click="thisComponent.parentComponent.fn()"></div> 代码不会中断,当componentOne是父级时,它实际上可以工作,但当它不是父级时,this.parentComponent计算结果为null,而不尝试componentTwo 更新2018-02-28 使用@Korte的答案(见下文)可以工作,但仍然想知道为什么上面的代码不起作用。当第一个返回null时
<div ng-click="thisComponent.parentComponent.fn()"></div>
代码不会中断,当componentOne
是父级时,它实际上可以工作,但当它不是父级时,this.parentComponent
计算结果为null
,而不尝试componentTwo
更新2018-02-28
使用@Korte的答案(见下文)可以工作,但仍然想知道为什么上面的代码不起作用。当第一个返回
null
时,它为什么不计算下一个选项?有人想解释一下吗 您可能需要两个父组件,然后检查定义了哪一个
e、 g
当然!有时候,事情并不像你想象的那么复杂。。谢谢!
app.component('thisComponent', {
controllerAs: 'thisComponent'
require: {
parentComponent : '?^componentOne' || '?^componentTwo'
},
controller: function() {
this.$onInit = () => {
// Returns null when the parent is componentTwo
console.log(this.parentComponent);
}
}
});
app.component('thisComponent', {
controllerAs: 'thisComponent',
require: {
firstParent : '?^componentOne',
secondParent: '?^componentTwo'
},
controller: function() {
this.$onInit = () => {
this.parentComponent = this.firstParent || this.secondParent;
console.log(this.parentComponent);
}
}
});