【发布时间】:2020-01-07 09:36:57
【问题描述】:
我有一个发出最新值的父组件,而在子组件中它使用@Input,其中不返回最新值。
基本上是这样开始的 在子组件中...
@Input() test;any;
我们确实将此测试绑定到 HTML 中,然后在按钮的单击事件中我们使用输入值 'test' 执行一些操作
onClickButton() {
this.test='your request is in progress'
....does some function where in it passes call to parent componentt and there it retrives the latest data of test so I am expecting that this.test would replaced by latest data from parent component.
}
但是,这里不是这种情况,它保留了“您的请求正在进行中......”
我在 ngOnChanges(changes:simpleChanges) 之类的子组件中进行了尝试,并尝试将 currentValue 分配给 this.test 值;但事情并没有朝着正确的方向发展。即使在父组件中,一切都正确处理我尝试打印最新值,但在子组件中它仍然是旧值。
我也试过了
this.ref.detectChanges();
this.ref.markforCheck();
但事情并没有帮助我从父组件获取更新值。
父母.ts
this.test ="从服务中获取价值,并且可以在调试器中看到该价值即将到来"
【问题讨论】:
-
给出你使用子组件的父模板文件结构..
-
@Input()仅在输入属性的引用发生更改时检查更改。如果您只是更改值,则对象的引用是相同的,因此 onChanges 无法正常工作..this.obj = { ...this.obj }这种解构语法您也更改了值和引用..现在 onChanges() 将被解雇.. -
请在主线程中查看更新。
-
在我的父组件中 this.test="last value from services" ,我在调试器本身中对此进行了验证。
-
即使当我将鼠标悬停在更改对象上时,我也可以看到 currentValue 是我期望的最新值,但是当我单击按钮时它打印值时 this.test="your request is in progress" ,我还是不明白这里出了什么问题。
标签: angular