【发布时间】:2016-11-04 15:49:48
【问题描述】:
我正在使用 Provider 注入器将命令从 Signin 组件发送到 Profile 组件以注销。尽管控制台日志显示 Signout 功能已按预期工作,但 (HTML) *ngIf 指令并未相应更改。
我做错了什么?
**Signin component**
import {Component, OnInit, Injectable } from '@angular/core';
import {ProfileUserComponent} from '../profile.user.component/profile.user.component'
@Component({
selector: 'signin-button',
template: `<button (click)="onSignOut()" *ngIf="loggedIn === true " >Sign Out</button>
<button (click)="onSignIn()" *ngIf="loggedIn !== true " >Sign In</button>
`,
providers: [ProfileUserComponent]
})
@Injectable()
export class SigninComponent {
loggedIn: boolean;
constructor(public http: Http, private profileUserComponent: ProfileUserComponent ){}
ngOnInit(): any{}
onSignOut(): void{
// send command to the profile component
this.profileUserComponent.signout(false);
}
}
**ProfileUserComponent**
import {Component, OnInit, Injectable} from '@angular/core';
@Component({
selector:'profile-user',
template: `<img class="ui mini image" src="image1.jpg" *ngIf="loggedIn === true">
<img class="ui mini image" src="image2.jpg" *ngIf="loggedIn === false">
`,
})
signout(status){
this.loggedIn = status;
console.log ("logged out", this.loggedIn); // Console log tells me that "Logged Out False"
}
}
【问题讨论】:
标签: angular typescript components