我想在Angular 2中使用模板驱动表单,并且我需要在我的指令中访问当前ngForm,作为本地属性,而不想将它们作为参数传递。
我的表单如下所示:
<form #frm="ngForm" (ngSubmit)="save(frm)">
<input [(ngModel)]="user.name" #name="ngForm" type="text">
<a (click)="showFrm()" class="btn btn-default">Show Frm</a>
</form>
并且在我的组件中
@Component({
selector: 'addUser',
templateUrl: `Templates/AddUser`,
})
export class AddUserComponent implements CanDeactivate {
public user: User;
// how can I use this without defining the whole form
// in my component I only want to use ngModel
public frm : ngForm | ControlGroup;
public showFrm() : void{
//logs undefined on the console
console.log(this.frm);
}
}
这可行吗,因为我需要在一个函数中检查myFrm是否有效或已被触摸,而我无法将当前表单作为参数传递,例如“routerCanDeactivate”,我也不想使用模型驱动的表单,因为这样写的代码太多了,我喜欢老派的ng1模型绑定。
我已更新我的示例,但组件中不知道frm。
AddUserComponent
应该作为表单的子元素放置在内部吗?- Felix