【发布时间】:2017-04-18 03:57:37
【问题描述】:
我有一个模态组件,它有一个表单和该表单之外的一些其他变量。组件是这样的:
组件:
export class PersonComponent implements OnInit {
countPerson: number=0;
persons: Person [] = [];
personForm : FormGroup;
ngOnInit(){
this.step1Form= this.fb.group({
firstName: 'Levi',
lastName: 'Ackerman'
});
}
submitPerson(person: Person){
this.persons.push(person);
this.incrementPerson();
}
incrementPerson(){
this.count++;
}
}
在模板中:
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body>
<label>First Name</label>
<input type="text" formControlName="firstName">
<label>LastName Name</label>
<input type="text" formControlName="lastName">
<button type="button" (click)="submitPerson()">Submit</button>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal">Close</button>
</div>
</div>
</div>
我想将表单控件重置回初始值,并将表单外部的变量设置回初始值。所以基本上我希望整个组件回到初始状态。我希望组件在关闭后重置为初始状态。
【问题讨论】:
-
您的模态从哪里启动?
-
模态框由父组件触发。所以modal是一个子组件
-
您是否尝试在模态窗口中编辑人员数据?
-
不只是使用表单将人员列表添加到人员数组中。关闭模式后,我希望表单和其他变量恢复到初始状态。因此,persons 数组、count person 和其他变量(我只是不包括它,因为它太多了)等变量回到了初始状态。有没有办法让整个组件恢复到初始状态。
-
目前正在发生什么。你能创建一个plunker吗?使用来自 answer 的模态作为示例
标签: angular typescript