我正在尝试从父组件接收更新(异步更新),但是当原始$ onInit从父组件接收数据时,任何更新都不会更新父组件(返回未定义状态)
有什么方式接收更新?
子组件:
import template from 'html-loader!./child.html'
export const childComponent = {
template: template,
require: {
parentComponent: '^parentComponent'
},
controllerAs: 'vm',
controller: class {
constructor($scope) {
this.option = null
this.items = []
this.$scope = $scope
}
static get $inject() {
return ['$scope']
}
$onInit() {
this.items = this.parentComponent.items
// this.items doesn't get updated when this.parentComponent updates
}
}
}
更新:
<div ng-app="app">
<div ng-controller="AppController as vm">
<parent-component items="vm.fruit">
<div ng-if="vm.fruit.length < 1">Loading...</div>
<child-component></child-component>
</parent-component>
</div>
</div>
答案 0 :(得分:1)
$onInit()
未被调用的原因是,您需要添加两件事:
在transclude: true
声明中添加parentComponent
,然后在html中添加ng-transclude
,如下所示:
<parent-component ng-transclude>
<child-component></child-component>
</parent-component>