0
旗帜HTML模板Angular2双重绑定不更新
body: {{body}}
<br>
message: {{message}}
<button type="submit" (click)="updateMessage('haha')">Update Message</button>
旗帜组件
import { Component } from '@angular/core';
import { StateService } from 'app/common/state.service';
@Component({
selector: 'banner',
templateUrl: 'app/banner/banner.component.html',
providers: [StateService]
})
export class BannerComponent {
body: string = 'This is the about home body';
message: string;
constructor(private stateService: StateService) {
}
ngOnInit() {
this.message = this.stateService.getMessage();
}
updateMessage(m: string): void {
this.stateService.setMessage(m);
}
}
import {Injectable} from '@angular/core';
@Injectable()
export class StateService {
private message = 'Hello Message';
getMessage(): string {
return this.message;
};
setMessage(newMessage: string): void {
console.error('setting message' + newMessage);
this.message = newMessage;
};
}
我以下的角2一些教程和我想要的状态服务有一个共享服务(通用状态),它具有可以从组件(标题)设置的属性。
一切都在编译,并且状态服务中的setter被正确的值触发。仅在banner.component.html中的双重绑定(消息:{{message}})未更新。 这是怎么回事?