我正在开发一个Angular2应用程序,其中有一个ConfigurationService
和一个组件,需要注册此服务的属性更改。Angular 2更新服务更新UI
import {Injectable} from "angular2/src/core/di/decorators";
@Injectable()
export class ConfigurationService {
private config = {
showDeveloperOptions: false
}
constructor() {}
get isDeveloper() {
return this.config.showDeveloperOptions;
}
public setDeveloperOptions(developerOptions: boolean) {
this.config.showDeveloperOptions = developerOptions;
console.warn("Set DeveloperOptions to " + this.config.showDeveloperOptions);
}
}
我的视图组件:
@Component({
selector: 'developer',
template: `<div [hidden]="config.isDeveloper">Not a Developer</div>`,
providers: [ConfigurationService],
})
export class DeveloperComponent {
constructor(public config: ConfigurationService) {
}
}
不幸的是,当我触发ConfigurationService.setDeveloperOptions(true)
从那里这项服务得到了注入另一个服务,它不更新视图。
我在做什么错?
非常感谢!
Seb
您如何将价值观融入您的观点? –
[hidden] =“config.isDeveloper”,config在组件构造函数中设置为公共可访问变量。 – Sebastian
我可能是错的,但我看到配置作为私人成员,并没有在构造函数?我知道你正在访问那个getter,因为它会发生什么,如果你只是声明var为public并直接绑定它(不使用getter)? –