2017-02-23 105 views
1

我想在我的ngclass中放2個條件,但它不工作任何人都可以請幫助我,thakns。如何把ngClass中的2個條件放在角度2

<input type="password" [ngClass]="{active: submitted && !form.controls['password']}" [formControl]="form.controls['password']" required> 
    this.submitted = false; 
onSubmit(form: any) { 
     this.submitted = true; 
} 

其中我的活動是一個帶有邊框錯誤的類,我試圖在點擊時顯示錯誤。

+1

將'active'改爲''active'' –

+0

我把[ngClass] =「{active:submitted &&!form.co ntrols ['password']。}}。我忘了有效..... – Daniel

+0

所以它沒有引號工作? –

回答

1

嗨,你可以試試這個!

對我很好!

ng-class=" {'active' : your condition, 'disabled' : your condition } " 
0
你不需要decleare如果使用表單控件的輸入標籤中需要

,你需要decleare它的TS裏面文件

HTML

<input type="password" [ngClass]="{'active': isActive()}" [formControl]="form.controls['password']"> 

打字稿

constractor(fb : FormBuilder){ 
    this.submitted = false; 
    this.form = fb.group({ 
     password: new FormControl({value: null}, Validators.compose([Validators.required])) 
    }); 
} 

onSubmit(form: any) { 
     this.submitted = true; 
} 

isActive(){ 
    return this.submitted && !this.form.controls['password']; 
}