我正在使用具有两种方式绑定(bind)概念的 Angular2
[(ngModel)]
。我的页面有表单,我必须验证元素的原始状态.因此,为了进行验证,我使用了 ngIf
来检查元素的原始状态。但条件不工作。我需要检查每个模型更改的原始状态。下面是我的 app.component.html
页面:
<form (ngSubmit)="angular2form(myAngular2Form.employeeDob)" [ngFormModel]="myAngular2Form">
<input type="text" class="form-control" id="employee" name="employee" [(ngModel)]="employeeDob" required />
<div *ngIf="employeeDob.pristine">
<p>Please enter the date</p>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
这是我的组件:
export class AppComponent {
employeeDob: String;
constructor(private myform: FormBuilder) {
this.employeeDob = '';
}
angular2form(date) {
alert("date submitted successfully");
}
}
谢谢你的建议
最佳答案
<input type="text" class="form-control" id="employee" name="employee" [(ngModel)]="employeeDob" #date="ngModel" required /><br/>
<div [hidden]="date.valid || date.pristine">
<p>Please enter the date</p>
</div>
直接输出文档
关于angularjs - 在 Angular 2 中,当我尝试使用双向绑定(bind)时 ngIF 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38693689/