javascript - 从 Angular 中的 @Input 更新 ngOnChanges View

标签 javascript angular javascript-objects angular-components ngonchanges

我在 Person 类型的子组件中有一个 @Input 属性,我通过属性从父组件传递对象

完整的工作代码在 StackBlitz 中可用。

我探讨了以下问题,我明白了他们在答案中所说的内容,但我根据答案尝试了 Object.assign 和其他东西,但它无法在 View 中加载数据。

如何通过@Input 传递对象,一旦对象到达子组件并需要在 View 中更新,我如何进行一些操作?

示例代码:

应用组件:

import { Component } from '@angular/core';

import { Person } from './models/person'

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  person: Person = {
    firstName: 'Emma',
    lastName: 'Watson'
  };
}

应用组件 HTML:

<user [user-profile]="person"></user>

用户组件:

import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Person } from '../models/person';

@Component({
  selector: 'user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.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;
    }
  }

}

用户组件 HTML:

Full Name: {{person.firstName}} {{person.lastName}}

@Input 收到对象后,我需要进行一些操作,并且需要在 UI 中更新它。我知道对象作为引用传递,但在这里我尝试使用 Object.assign 并使用 undefined 分配属性,然后适当的对象没有任何工作。

最佳答案

ngOnInit() 中删除 person 的分配,ngOnInit 在 ngOnChanges 之后运行,因此您将值恢复为空

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.com/edit/angular-input-ng-onchange-qiyghn?file=src%2Fapp%2Fuser%2Fuser.component.ts

关于javascript - 从 Angular 中的 @Input 更新 ngOnChanges View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55495333/

相关文章:

javascript - 页面如何知道我正在用 Firebug 分析它

javascript - 安装vuejs后在数据对象中设置地理位置

angular - RxJS 和 Angular 6 - 可观察对象、主题和基本的获取和添加数组操作

javascript - $parse 的 Angular2 等价物是什么?

angular - 如何在 Webpack 中使用 tree shaking?

javascript - 删除对象定义内的属性;为什么?

Javascript 动态变量名称不起作用

javascript - 通过过滤器数组设置对象数组的属性

javascript - 在 JavaScript 中使用 {} 或 new Object() 创建一个空对象?

javascript - 将长文本从脚本浏览器 (phantomjs) 发布到 php 脚本不会超过 2kb/2400 个字符