小编Yas*_*aga的帖子

父变量未在 Angular 的子组件中更新

我在父组件中有一个这样的变量:

父组件

export class ParentComponent {
    
  variable = {};
  varExample = false;
  @ViewChild('child') child: ChildComponent;
  
  someFunction () {
    this.variable['id'] = {};
    for (let i = 0; i < 10; i++) {
      this.variable['id'][i] = i*2; 
    }
    this.varExample = true;
  }

  otherFunction () { // called when i click a button
    this.someFunction ();
    
    console.log(this.child.variable); // {}
    console.log(this.child.varExample); // true
    this.child.initVars();
  }
}
Run Code Online (Sandbox Code Playgroud)

父 HTML

<app-child #child [variable]="variable" [varExample]="varExample"></app-child>
Run Code Online (Sandbox Code Playgroud)

子组件

export class ChildComponent {
  @Input() variable: any;
  @Input() varExample: boolean;

  initVars() {
    console.log(this.variable); …
Run Code Online (Sandbox Code Playgroud)

input typescript angular

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

angular ×1

input ×1

typescript ×1