admin管理员组文章数量:1292227
There are 2 ponents: parentComponent and ChildComponent, which is defined inside the parent. In the parentComponent there is a local variable which is used as value to pass to an input property of the ChildComponent (using a getter).
ParentComponent.ts:
@Component({
selector:'parent-ponent',
template:`
<h1>parent ponent</h1>
<child-ponent [personData]="PersonData"></child-ponent>
`
})
export class ParentComponent{
personData:Person;
get PersonData():Person{
return this.personData;
}
set PersonData(person:Person){
this.personData = person;
}
ngOnInit(){
this.PersonData = new Person();
this.PersonData.firstName = "David";
}
//more code here...
}
ChildComponent.ts:
@Component({
selector:'child-ponent',
template:`
<h1>child ponent</h1>
<div *ngIf="personData">{{personData.firstName}}</div>
`
})
export class ChildComponent{
@Input() personData:Person;
//more code here...
}
The issue is: in some place in the parent ponent, when specific event occurs, a function newPersonArrived(newPerson:PersonData) is being called, the function code is as following:
newPersonArrived(newPerson:Person){
this.PersonData = newPerson;
}
This doesn't affect the UI with the new person name!
Only the following helps:
newPersonArrived(newPerson:Person){
this.PersonData = new Person();
this.PersonData.firstName = newPerson.firstName;
}
Is this the expected behavior?
why only when the personData is initialized to new Person, the UI "catches" the change?
There are 2 ponents: parentComponent and ChildComponent, which is defined inside the parent. In the parentComponent there is a local variable which is used as value to pass to an input property of the ChildComponent (using a getter).
ParentComponent.ts:
@Component({
selector:'parent-ponent',
template:`
<h1>parent ponent</h1>
<child-ponent [personData]="PersonData"></child-ponent>
`
})
export class ParentComponent{
personData:Person;
get PersonData():Person{
return this.personData;
}
set PersonData(person:Person){
this.personData = person;
}
ngOnInit(){
this.PersonData = new Person();
this.PersonData.firstName = "David";
}
//more code here...
}
ChildComponent.ts:
@Component({
selector:'child-ponent',
template:`
<h1>child ponent</h1>
<div *ngIf="personData">{{personData.firstName}}</div>
`
})
export class ChildComponent{
@Input() personData:Person;
//more code here...
}
The issue is: in some place in the parent ponent, when specific event occurs, a function newPersonArrived(newPerson:PersonData) is being called, the function code is as following:
newPersonArrived(newPerson:Person){
this.PersonData = newPerson;
}
This doesn't affect the UI with the new person name!
Only the following helps:
newPersonArrived(newPerson:Person){
this.PersonData = new Person();
this.PersonData.firstName = newPerson.firstName;
}
Is this the expected behavior?
why only when the personData is initialized to new Person, the UI "catches" the change?
Share edited May 9, 2017 at 6:23 tanmay 7,9112 gold badges23 silver badges39 bronze badges asked May 9, 2017 at 6:23 BatshevaBatsheva 6694 gold badges13 silver badges30 bronze badges 6-
Where do you call
newPersonArrived
? It should work – yurzui Commented May 9, 2017 at 6:26 - After some event occurs in the parent ponent, I see that the functin is being called (I put there console.log('aa') and saw it was printed) – Batsheva Commented May 9, 2017 at 6:28
- plnkr.co/edit/0SVsezfJYtc0xzKGkAZe?p=preview – yurzui Commented May 9, 2017 at 6:28
- I see the example, so do you mean this is depends from which event I am ing? sounds interesting – Batsheva Commented May 9, 2017 at 6:46
- I don't know exactly. To better understand it we need some working example – yurzui Commented May 9, 2017 at 6:48
1 Answer
Reset to default 7please watch for your changes in child ponent
import { Component, Input, Output, OnChanges, EventEmitter, SimpleChanges } from '@angular/core';
@Component({
selector:'child-ponent',
template:`
<h1>child ponent</h1>
<div *ngIf="personData">{{personData.firstName}}</div>
`
})
export class ChildComponent implements OnChanges{
@Input() personData:Person;
public ngOnChanges(changes: SimpleChanges) {
if ('personData' in changes) {
//some code here
}
}
//more code here...
}
本文标签: javascriptAngular 4 Input property update does not affect the UIStack Overflow
版权声明:本文标题:javascript - Angular 4 @Input property update does not affect the UI - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741547534a2384682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论