我有一個指令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相同的錯誤 –