我有 2 个文件。app.ts 和 Child.ts
我正在从应用程序向孩子发送一个变量,我想检测变量的任何变化并相应地显示数据。我无法检测到变量的变化。
有什么帮助吗?我附上了 Plunker Link 并且我还在评论中解释了我想在 Child.ts 文件中执行的操作
应用程序.ts 文件
//our root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {ChildCom} from './child.ts'
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello</h2>
<child-com newdata={{data}}></child-com>
</div>
`,
})
export class App {
data: any = [];
constructor(){
this.data = [{"name":"control","status":"false"}];
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, ChildCom ],
bootstrap: [ App ]
})
export class AppModule {}
Child.ts 文件
import {Component, Input} from '@angular/core';
@Component({
selector: 'child-com',
template: `
<div>
<p>Controls: {{newdata}}</p>
</div>
`,
})
export class ChildCom {
@Input() newdata: any = [];
constructor(){
}
// here I want to check if the value of control in newdata variable is false
// then display a message on the front end "your controls are not working"
// if the value of control in newdata variable is true
// then display a message on front end "your controls are working fine."
// this should automatically happen whenever I change the value of newdata variable
}