admin管理员组文章数量:1332873
I have a @Input property in my child ponent of type Person and I'm passing the object from the parent ponent through an attribute
Full working code is available in StackBlitz
I explored the following question I got the point that what they told in the answer but I tried the Object.assign and other things based on the answer but it fails to load the data in View.
- Angular: ngOnChanges not updating value
How can I pass the Object through @Input and how can I do some manipulation once the object reached to the child ponent and need to update in the view?
Sample Code:
App Component:
import { Component } from '@angular/core';
import { Person } from './models/person'
@Component({
selector: 'my-app',
templateUrl: './appponent.html',
styleUrls: [ './appponent.css' ]
})
export class AppComponent {
person: Person = {
firstName: 'Emma',
lastName: 'Watson'
};
}
App Component HTML:
<user [user-profile]="person"></user>
User Component:
import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Person } from '../models/person';
@Component({
selector: 'user',
templateUrl: './userponent.html',
styleUrls: ['./userponent.css']
})
export class UserComponent implements OnInit, OnChanges {
@Input('user-profile') profile: Person;
person: Person;
constructor() {}
ngOnInit() {
this.person = {
firstName: '',
lastName: ''
}
}
ngOnChanges(changes:SimpleChanges): void {
if(typeof this.profile !== 'undefined'
&& typeof this.profile.firstName !== 'undefined'
&& typeof this.profile.lastName !== 'undefined') {
this.person.firstName = this.profile.firstName;
this.person.lastName = this.profile.lastName;
}
}
}
User Component HTML:
Full Name: {{person.firstName}} {{person.lastName}}
I need to do some manipulation once I received the object from @Input
and need to update it in the UI. I know the object is passing as reference but here I tried with Object.assign
and I assigned the property with undefined
and then the appropriate object nothing is working.
I have a @Input property in my child ponent of type Person and I'm passing the object from the parent ponent through an attribute
Full working code is available in StackBlitz
I explored the following question I got the point that what they told in the answer but I tried the Object.assign and other things based on the answer but it fails to load the data in View.
- Angular: ngOnChanges not updating value
How can I pass the Object through @Input and how can I do some manipulation once the object reached to the child ponent and need to update in the view?
Sample Code:
App Component:
import { Component } from '@angular/core';
import { Person } from './models/person'
@Component({
selector: 'my-app',
templateUrl: './app.ponent.html',
styleUrls: [ './app.ponent.css' ]
})
export class AppComponent {
person: Person = {
firstName: 'Emma',
lastName: 'Watson'
};
}
App Component HTML:
<user [user-profile]="person"></user>
User Component:
import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Person } from '../models/person';
@Component({
selector: 'user',
templateUrl: './user.ponent.html',
styleUrls: ['./user.ponent.css']
})
export class UserComponent implements OnInit, OnChanges {
@Input('user-profile') profile: Person;
person: Person;
constructor() {}
ngOnInit() {
this.person = {
firstName: '',
lastName: ''
}
}
ngOnChanges(changes:SimpleChanges): void {
if(typeof this.profile !== 'undefined'
&& typeof this.profile.firstName !== 'undefined'
&& typeof this.profile.lastName !== 'undefined') {
this.person.firstName = this.profile.firstName;
this.person.lastName = this.profile.lastName;
}
}
}
User Component HTML:
Full Name: {{person.firstName}} {{person.lastName}}
I need to do some manipulation once I received the object from @Input
and need to update it in the UI. I know the object is passing as reference but here I tried with Object.assign
and I assigned the property with undefined
and then the appropriate object nothing is working.
2 Answers
Reset to default 4Remove the asignment of person
from ngOnInit()
, ngOnInit runs after ngOnChanges
so you are restting the values back to empty
export class UserComponent implements OnInit, OnChanges {
@Input('user-profile') profile: Person;
person: Person = { firstName: '', lastName: '' }; // initialize it here
constructor() {}
ngOnInit() {
// this.person = {
// firstName: '',
// lastName: ''
// }
}
ngOnChanges(changes:SimpleChanges): void {
if(typeof this.profile !== 'undefined'
&& typeof this.profile.firstName !== 'undefined'
&& typeof this.profile.lastName !== 'undefined') {
console.log(this.profile)
this.person.firstName = this.profile.firstName;
this.person.lastName = this.profile.lastName;
console.log(this.person)
}
}
}
https://stackblitz./edit/angular-input-ng-onchange-qiyghn?file=src%2Fapp%2Fuser%2Fuser.ponent.ts
It's because ngOnInit
is ran after ngOnChanges
. So you first set it, but then immediately reset it inside your ngOnInit
.
See here a working example.
Basically change your ponent person attribute to this:
person: Person = {
firstName: '',
lastName: ''
};
and remove the ngOnInit
.
You can also use a setter on the profile input, this way you don't need ngOnChanges
:
@Input('user-profile') set profile(person: Person) {
this.person.firstName = profile && profile.firstName || '';
this.person.lastName = profile && profile.lastName || '';
}
person: Person = {
firstName: '',
lastName: ''
};
本文标签: javascriptUpdate view on ngOnChanges from Input in AngularStack Overflow
版权声明:本文标题:javascript - Update view on ngOnChanges from @Input in Angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742308341a2450367.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论