Angular2 - ngIf不会重新呈现子组件

时间:2017-01-16 13:03:33

标签: angular

我有一个登录组件,它是从多个地方调用的,并有一个输入将其显示为模态或只是普通组件。

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');
        }
    }
}

我的Login / index.html模板包含一个简单的bootstrap模式。

我的父组件:

@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',具体取决于用户是否经过身份验证,并且* ngIf使用模态选项重新呈现LoginComponent。我希望角度重绘时* ngIf更新并重新打开模态,但事实并非如此。它只能工作一次(即使我将showLoginModal重置为false,然后再将其设置为true)。

1 个答案:

答案 0 :(得分:2)

注入cdRef:ChangeDetectorRef

constructor(private cdRef:ChangeDetectorRef)  {}

并将showLoginModal设置为false,然后将true设置为this.showLoginModal = false; this.cdRef.detectChanges(); this.showLoginModal = true; 之间的呼叫更改检测,如

ngIf

即使在调用showLoginModal方法之前truesubmitBid()def lastline(fil): with open(fil) as f: for li in f.readlines(): if li.startswith("Final Value:"): print(li) ## If it still doesnt work try putting 'dirs=[]' here def lookforfiles(basepath): contents = os.listdir(basepath) dirs = [] i = 0 while i <= len(contents): i += 1 for n in contents: f = os.path.join(basepath, n) if os.path.isfile(f): lastline(f) print("\n\nfile %s" % n) elif os.path.isdir(f): print("Adding dir") if f in dirs: pass else: dirs.append(f) else: for x in dirs: print("dir %s" % x) lookforfiles(x) 也会重新呈现。