我有一个公开的方法,我接触过window
。此方法与 a 对话Component
并修改我在模板中查看的变量。但是当我更改值时,*ngIf()
不会触发。
应用程序组件
constructor(private _public: PublicService,) {
window.angular = {methods: this._public};
}
公共服务
export class PublicService {
constructor(
private _viewManager: ViewManagerComponent,
) {}
CallMe(){
this._viewManager.renderView('page1')
}
}
布局管理器组件
@Component({
selector: 'view-manager',
template: `<page *ngIf="view == 'page1'"></page>`
})
export class ViewManagerComponent {
//This is the variable being watched
view = "page";
renderView = function(type){
console.log(type)
this.view = type;
console.log(this.view)
};
}
所以想法是,当视图最初加载时,视图是空白的。然后,当我键入时angular.methods.CallMe()
,它会修改view
变量,page1
然后应该显示组件的 html。如果我控制台renderView
函数成功被调用,只是视图不会改变。
----更新 - 仍然无法正常工作-----
export class ViewManagerComponent {
constructor(private zone:NgZone,private cdRef:ChangeDetectorRef) {
}
view = "page";
@Output() renderView(type){
// type is 'page'
console.log(this.view)
this.zone.run(() => {
// type is 'page'
console.log(this.view)
this.view = type;
// type is 'page1'
console.log(this.view)
});
// type is 'page1'
console.log(this.view)
//cdRef errors:
//view-manager.component.ts:36 Uncaught TypeError: this.cdRef.detectChanges is not a function(…)
this.cdRef.detectChanges();
};
}