2017-01-16 21 views
1

私は複数の場所から呼び出されているログインコンポーネントを持っており、モーダルまたは単純なコンポーネントとして表示するための入力があります。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に設定しても)。

+1

'cdRef:ChangeDetectorRef'を注入して' false'に設定してから 'this.showLoginModal = false;のように変更検出を呼び出すと' true'にしてみてくださいthis.cdRef.detectChanges(); this.showLoginModal = true ; ' –

+0

showLoginModalをPUBLICに設定しようとしましたか? –

+0

@federicoscamuzzi公衆に設定しても機能しませんでした。 –

答えて

2

cdRef:ChangeDetectorRef

constructor(private cdRef:ChangeDetectorRef) {} 

を注入し、falseshowLoginModalを設定し、

等との
this.showLoginModal = false; 
this.cdRef.detectChanges(); 
this.showLoginModal = true; 

で呼び出す変化検出とtrueこの道ngIfは再レンダリングされますshowLoginModalは前trueした場合でも、 submitBid()メソッドが呼び出されました。

+1

それは働いて、ありがとう! –

+0

フィードバックをいただきありがとうございます。 –

関連する問題