以下是我正在做的示例代码。
模型组件
//app.models.ts
export class Employee{
Name:string;
Salary:number;
}
根组件
//app.component.ts
import { Component } from '@angular/core';
import { Employee } from './app.models.ts';
@Component({
selector: 'my-app',
template: '<input type="text" [(ngModel)]="emp.Name"><hello [employee]="emp"></hello>'
})
export class AppComponent {
emp:Employee;
constructor(){
this.emp=new Employee();
this.emp.Name="Neeraj";
}
}
子组件
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Employee } from './app.models.ts';
@Component({
selector: 'hello',
template: `<h1>{{displayName}}</h1>`,
styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent implements OnChanges {
@Input() employee: Employee;
displayName:string;
ngOnChanges(changes: SimpleChanges) {
console.log('Life cycle hook method called.');
this.displayName='Hello, '+this.employee.Name;
}
}
子组件中未检测到文本框文本更改。