2016-04-13 16 views
2

私のコードでは、フォームが無効であるか汚れていない場合でも無効になるボタンがあります。Angular2と無効化ボタンの例外

私が持っている:

例外スロー

<button type="submit" [disabled]="!myForm.valid || myForm.dirty" class="save-button">Save</button> 

:!式」をmyForm.valid || MyFromComponent @ 58:38 '内のチェックされた後に変更されました。以前の値: 'true'現在の値:[!myForm.valid ||で 'false' MyFromComponentのmyForm.dirty @ 58:38]

妥当性/汚れが変更されたときはいつでも。

アイデア?

UPDATE

私はプロダクションモードを有効にすると、それが機能するには:

enableProdMode() 
+0

は、なぜあなたは単に '使用しないplunker例のClick here!myForm.valid '? – micronyks

+0

@micronyks、そうでなければなりません!myForm.valid || myForm.dirty。あなたは '[disabled] ="!(myForm.valid || myForm.dirty) "を' '(' ...)にラップしようとしたのですか? – uksz

+0

を保存できませんでした。 '... – micronyks

答えて

3

私は、これは、同様の目的を果たすだろうと思います。

[disabled]="!myForm.valid || !myForm.dirty" 
+0

はうまくいきませんでしたが、エラーはまだ発生します – uksz

+1

http://plnkr.co/edit/Wl6lDyFhhpNt8DFUEdWA?p=preview私の実装でうまくいくように見えます。 – micronyks

1

これは連絡フォームの例です。これを確認してください。

HTMLファイル

<form (ngSubmit)="contactSubmit()" #contactForm="ngForm"> 
    <div class="form-group"> 
     <label for="exampleInputEmail1">Name</label> 
     <input type="text" class="form-control" placeholder="Name" required ngControl="name" #name="ngForm" [(ngModel)]="cForm.name"> 
    </div> 
    <div class="form-group"> 
     <label for="exampleInputEmail1">Email address</label> 
     <input type="email" class="form-control" placeholder="Email" required ngControl="email" #email="ngForm" [(ngModel)]="cForm.email"> 
    </div> 
    <div class="form-group"> 
     <label for="exampleInputEmail1">Message</label> 
     <textarea class="form-control" placeholder="Message" required ngControl="message" #message="ngForm" [(ngModel)]="cForm.message" ></textarea> 
    </div> 
    <button type="submit" class="btn btn-default" [disabled]="!contactForm.form.valid">Submit</button> 
    <button type="button" class="btn btn-default" (click)="newcForm()" >New Form</button> 
</form> 

TSファイル

import {Component} from 'angular2/core'; 
import {NgForm} from 'angular2/common'; 

@Component({ 
    selector: 'my-app', 
    providers: [], 
    templateUrl: 'contact-us.component.html', 
    directives: [] 
}) 
export class App { 
    public cForm = new contactForm('','',''); 
    submitted = false; 
    active = true; 

    contactSubmit(){ 
     this.submitted = true; 
    } 
    newcForm(){ 
     this.cForm = new contactForm('','',''); 
     this.active = false; 
     setTimeout(()=> this.active=true, 0);   
    } 
} 

export class contactForm { 
    constructor(
     public name: number, 
     public email: string, 
     public message: string 
    ) { } 
} 

関連する問題