@Component({
selector: 'four',
directives: [MyDirective],
template: `
Hello, this is working! <br>
<textarea mydir [(ngModel)]="pp.status" [ngFormControl]="myCtrl">
</textarea>
`,
})
export class Four {
@Input() pp;
constructor() {
this.myCtrl = new Control();
this.myCtrl.valueChanges.subscribe(
(data) => {
console.log('Model change');
});
}
}
您还可以利用自定义 EventEmitter ,您可以在服务中订阅以通知组件外部 .
1
您可以使用rx.js函数 Observable.of(something) .
import {Observable} from "rxjs/Rx";
class Example {
public test;
public watchTest;
constructor(){
this.setWatch()
this.seeWatch()
}
setWatch() {
this.watchTest = Observable.of(this.test);
}
seeWatch() {
this.watchTest.subscribe(data => {
data //this.test value
}
}
}
2 回答
如果任何绑定已更改,您可以使用
ngOnChanges
方法通知:但这取决于您的用例,因为您也可以利用(例如)表单控件进行通知:
您还可以利用自定义
EventEmitter
,您可以在服务中订阅以通知组件外部 .您可以使用rx.js函数
Observable.of(something)
.