小编Joh*_*han的帖子

我可以“观察”组件的动态子代吗?

我有一个应用程序,我在其中使用多个表单组件组合表单。现在我想创建某种外键字段,它列出了已经存在的对象并显示了一个“添加”按钮。此按钮在模式对话框中显示另一个表单组件。该“子”表单仅使用<ng-content>. 这一切都很完美

我来说明一下情况。这是模板<form-component>

<form class="form">
    <form-field>
        <another-form-component (save)="handleSave()"></another-form-component>
    </form-field>
</form>
Run Code Online (Sandbox Code Playgroud)

的模板<form-field>

<modal-dialog-component [(visible)]="showForm">
    <!--
        Here the <another-form-component> which was passed
        to <form-field> is added:
    -->
    <ng-content></ng-content>
</modal-dialog-component>
<div class="form-field">
    <select> <!-- existing options --> </select>
    <button (click)="openForm($event);">Create new</button>
</div>
Run Code Online (Sandbox Code Playgroud)

如您所见,<another-form-component>有一个@Output()for itsave事件。这是一个EventEmitter.

我的问题是:如何EventEmitter<form-field>组件订阅此内容?我想知道表单何时保存,以便我可以关闭<modal-dialog>.

请记住:表单是使用<ng-content>. 我可以@ViewChildren用来获取孩子<form-field>并使用某种addEventListener()方法吗?这样的东西真的存在吗?

希望你能帮我!

你好,约翰

javascript angular2-ngcontent angular

5
推荐指数
1
解决办法
1170
查看次数

Angular 2:向ngModelGroup添加验证器

我正在使用一个ngModelGroup指令将几个表单输入组合在一起.

在文档(https://angular.io/docs/ts/latest/api/forms/index/NgModelGroup-directive.html)中,我读到有一个validators: any[]属性.

这是否意味着我可以添加自定义验证器功能,仅验证该功能ngModelGroup?如果是这样,我该如何使用它?

这将是非常棒的,因为我想检查是否至少检查了其中一个复选框ngModelGroup.我无法使用,required因为这意味着所有复选框都是必需的.我在文档中找不到任何相关内容,或者我找错了地方?

javascript validation angular2-forms angular

4
推荐指数
1
解决办法
4474
查看次数