私は複数の場所から呼び出されているログインコンポーネントを持っており、モーダルまたは単純なコンポーネントとして表示するための入力があります。Angular2 - ngIfは子コンポーネントを再レンダリングしません
LoginComponent:
declare var jQuery: any;
@Component({
selector: 'login-view',
templateUrl: '/login/index.html',
inputs: ['isOpenLoginModal']
})
export class LoginComponent extends BaseConfigurations {
public isOpenLoginModal: boolean;
//this method is called from ngOnInit in base class
public initialize() {
this.showModal();
}
constructor() {
super();
}
private showModal(): void {
if (this.isOpenLoginModal) {
jQuery("#loginComponentModal").modal('show');
}
}
}
マイログイン/ index.htmlのテンプレートは、単純なブートストラップモーダルが含まれています。
私の親コンポーネント: 私はのように私の親のコンポーネントに自分のログイン・コンポーネントを配置している@Component({
selector: 'upcoming-auction-view',
templateUrl: '/auctions/upcoming-auctions.html'
})
export class UpcomingAuctionsComponent extends BaseConfigurations {
private showLoginModal: boolean = false;
public initialize() {
this.showLoginModal = false;
}
constructor() {
super();
}
submitBid(): void {
if (!this.isAuthenticated) {
//if user is not authenticated, show login component
this.showLoginModal = true;
}
}
}
:
<login-view *ngIf="showLoginModal === true" [isOpenLoginModal]="true"></login-view>
とsubmitBid関数を呼び出し、フラグを更新ボタンを持って
<input type="submit" class="form-submit primary-btn" value="Place Bid" (click)="submitBid()" />
私は、ユーザーが認証されているかどうかに基づいて 'showLoginModal'を更新しています。 otを使用し、* ngIを使用してLoginComponentをモーダルオプションで再レンダリングしてください。 * ngIfが更新され、モーダルを再度開いたときに、角度が再描画されることが期待されますが、そうではありません。それは一度だけ機能します(showLoginModalをfalseにリセットしてからtrueに設定しても)。
'cdRef:ChangeDetectorRef'を注入して' false'に設定してから 'this.showLoginModal = false;のように変更検出を呼び出すと' true'にしてみてくださいthis.cdRef.detectChanges(); this.showLoginModal = true ; ' –
showLoginModalをPUBLICに設定しようとしましたか? –
@federicoscamuzzi公衆に設定しても機能しませんでした。 –