我有一个指令app.service.ts
存储应用程序状态数据,以及我试图使用从其他组件这个指令,所以我可以获取和设置应用程序的状态(这是工作)。angular2属性更改错误
但是它给了我这个错误,当我尝试从本指令的属性绑定到视图
EXCEPTION: Expression 'loading: {{loadState}} in [email protected]:23' has changed after it was checked. Previous value: 'false'. Current value: 'true' in [loading: {{loadState}} in [email protected]:23]
这里我想显示加载文本,当appState.get().loadState == true
应用.service.ts - source
import {Injectable} from 'angular2/core';
import {WebpackState} from 'angular2-hmr';
@Injectable()
export class AppState {
_state = {}; // you must set the initial value
constructor(webpackState: WebpackState) {
this._state = webpackState.select('AppState',() => this._state);
}
get(prop?: any) {
return this._state[prop] || this._state;
}
set(prop: string, value: any) {
return this._state[prop] = value;
}
}
app.ts
import {AppState} from './app.service';
export class App{
constructor(public appState: AppState) {
this.appState.set('loadState', false);
}
get loadState() {
return this.appState.get().loadState;
}
}
app.html
<div class="app-inner">
<p>loading: {{loadState}}</p>
<header></header>
<main layout="column" layout-fill>
<router-outlet></router-outlet>
</main>
</div>
app.ts
假设有一个子组件home.ts
并加载到视图
home.ts
export class HomeCmp {
page;
constructor(private wp: WPModels, private appState: AppState) {}
ngOnInit() {
var pageId = this.appState.get().config.home_id;
this.appState.set('loadState', true); // before http request
this.wp.fetch(WPEnpoint.Pages, pageId).subscribe(
res => {
this.page = res;
this.appState.set('loadState', false); // after http request
},
err => console.log(err)
);
}
}
,可以使用共享对象此。我认为HTML中的{{loadState}}总是会以这种方式抛出错误。而是尝试绑定在服务中使用的一些共享变量。例如。 _state = {}是一个共享对象。设置一些属性,然后在HTML中,你可以通过{{appState._state.someProperty}}来访问它' – micronyks
@micronyks相同的错误 –