实现这一点的一种方法是挂钩到 Angular 的新组件方法,即$onChanges
和$onInit
.
如果products.list.component.js
确实对 products 对象进行 API 调用,然后呈现一个新的$ctrl.results
数据集。
您可以让组件使用新方法product-result.js
检查单向绑定。 每当更新单向绑定时调用。该方法接受一个更改对象参数。更改对象的键是已更改的绑定属性的名称。$ctrl.results
$onChanges
$onChanges
你的代码product-result.js
可能是
/**
* @ngdoc function
* @name $onInit
* @description on bootstrap for the component check if the value `ctrl.results ` is falsy
*/
$onInit: function $onInit () {
if (!this.results) {
this.showNoDataAvaliable = true;
}
},
/**
* @ngdoc function
* @name $onChanges
* @description when component product-list passes a new value for $ctrl.results, Show the data.
* @param {Object} changes the changes object when any binding property is updated, this is similar to
* having a watch function on $scope.variable
*/
$onChanges: function $onChanges (changes) {
if (changes.data && changes.data.currentValue) {
this.showNoDataAvaliable = false;
//... more code on how to display the data.
}
}
Todd Motto 有一篇关于 Angular 1.5 组件的精彩博客文章,我建议您阅读
https://toddmotto.com/angular-1-5-lifecycle-hooks