javascript - 为什么 ngOnChanges() 函数没有在 Angular 2 中触发?

标签 javascript angularjs angular angular2-routing angular2-template

请告诉我原因 ngOnChanges()函数没有在 angular2 中触发? 我尝试更改 message属性(property)使用 @Input但 onchange 事件没有被触发。

这是我的代码 https://plnkr.co/edit/BOw5vazUUM8WNh9C1CNa?p=preview

export class Test implements OnInit,OnChanges{
  name:string;
  @Input() message;
  constructor() {
    console.log('==constructor==')
    this.name = 'Angular2'
  }
  ngOnInit(){
     // this.myService.someMethod() 
       console.log('==ngOnInit==')
   }

    ngOnChanges() {
        console.log('onChange fired');

    }
}

最佳答案

我猜你想在父级和子级之间绑定(bind)数据,当子级更改消息时,父级将收到更新的值。

这是演示:

app.ts

//our root app component
import {Component, NgModule,OnInit,OnChanges} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import {Test} from './test';

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}} from parent</h2>
      <test [(message)]="name"></test>
    </div>
  `,
})
export class App implements OnInit,OnChanges{
  name:string;
  constructor() {
    console.log('==constructor==')
    this.name = 'Angular2'
  }
  ngOnInit(){
     // this.myService.someMethod() 
       console.log('==ngOnInit==')
   }

    ngOnChanges() {
        console.log('onChange fired');

    }
}

@NgModule({
  imports: [ BrowserModule,FormsModule  ],
  declarations: [ App ,Test],
  bootstrap: [ App ]
})
export class AppModule {}

测试.ts

import { Component, NgModule,OnInit,OnChanges,Input, Output, EventEmitter } from '@angular/core'

@Component({
  selector: 'test',
  template: `
    <div>
     {{message}}
     <input type="text" [ngModel]= "message" (ngModelChange)="onModelChange($event)"/>
    </div>
  `,
})
export class Test implements OnInit,OnChanges{
  name:string;
  @Input() message;

  @Output() messageChange = new EventEmitter('');
  constructor() {
    console.log('==constructor==')
    this.name = 'Angular2'
  }
  ngOnInit(){
     // this.myService.someMethod() 
       console.log('==ngOnInit==')
   }

    ngOnChanges() {
        console.log('onChange fired');

    }
  onModelChange(event) {
    this.message = event;

    this.messageChange.emit(this.message);
  }
}

演示:https://plnkr.co/edit/nU9Qkuf7bwgSga7Ce2Wc?p=preview

这是 Angular 中的自定义双向绑定(bind)。

<test [(message)]="name"></test>

等效:

<test [message]="name"(messageChange)="message = $event"></test>

其中 messageChange 是来自子组件的事件。

关于javascript - 为什么 ngOnChanges() 函数没有在 Angular 2 中触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42201114/

相关文章:

javascript - 图像已正确上传但在刷新之前未在 View 中更新

angularjs - 如何使用 angularjs $resource 进行服务器端分页?

angular - 将数组中的值附加到 BehaviorSubject

javascript - 替换功能怎么可能以不同的方式工作?

javascript - 我正在尝试创建一台使用机器人类来玩卡住标签的计算机

Javascript/JQuery 故障排除

Angular2 RC5 强制将组件导入移动到 NgModule 声明 - 为什么?

javascript - 返回无效 0;与返回;

javascript - AngularJS 将对象数组中的选定选项绑定(bind)到仅存储 id 的模型

angular - Angular 中 json 映射的两种方式绑定(bind)