简体   繁体   English

触发Angular App中的$ onChanges

[英]Triggering $onChanges in Angular App

I have a component "MealItemCollection" with an $onChanges function in my Angular app. 我的Angular应用程序中有一个带有$ onChanges函数的组件“ MealItemCollection”。 The component is used by two separate parent components, "Recipe" and "MealPlan". 该组件由两个单独的父组件“ Recipe”和“ MealPlan”使用。 Both parent components pass in an "items" prop the exact same way. 两个父组件都以完全相同的方式传递“项目”道具。

However, while the $onChanges function works properly when changes to items are passed from the MealPlan component, it only works 1 time when changes to items are passed from the Recipe component. 但是,虽然从MealPlan组件传递对项目的更改时$ onChanges函数可以正常工作,但是从Recipe组件传递对项目的更改时,它仅工作1次。 That is, when I have 0 items and add 1, the $onChanges triggers, but when I have 1 item and add to it, the $onChanges does not trigger. 也就是说,当我有0个项目并加1时,$ onChanges会触发,但是当我有1个项目并加到其中时,$ onChanges不会触发。

In the recipe parent component, I have a "recipeModel" object with an items attribute. 在配方父组件中,我有一个带有items属性的“ recipeModel”对象。 When I add an item, it does in fact add an item to recipeModel.items, as verified by console.logs. 当我添加一个项目时,它实际上确实在console.logs所验证的项目中添加了一个项目。 But for whatever reason the changes to the items prop are not being passed down to the MealItemCollection component. 但是无论出于什么原因,都不会将对道具属性的更改传递给MealItemCollection组件。

I have attempted subbing out $onChanges with $doCheck but that is not working because, like I said, the changes to the items prop are not being passed down into the MealItemCollection component. 我试图用$ doCheck减去$ onChanges,但是那是行不通的,因为,就像我说的那样,对item道具的更改没有被传递到MealItemCollection组件中。

Does anybody have any idea what could possibly be going on here? 有人知道这里可能发生什么吗?

For some reason this code: 由于某种原因,此代码:

ctrl.recipeModel.ingredients = _.clone(ctrl.recipeModel.ingredients);

after the recipeModel has been updated and that is enough to trigger the onChanges in the child component. 更新了modelModel之后,足以触发子组件中的onChanges。

Does anybody have any idea what is going on here? 有人知道这里发生了什么吗?

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

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