【问题标题】:How to share the data up from transcluded content angular 4如何从嵌入的内容角度 4 共享数据
【发布时间】:2018-05-07 19:16:58
【问题描述】:

我有一个通用模式

<div class="modal-header">
  <h1 mat-dialog-title [innerHTML]="data.title"></h1>
</div>
<div mat-dialog-content>
 <ng-content></ng-content>
</div>
<mat-dialog-actions>
 <button></button>
</mat-dialog-actions>

我有模态的内容

<modal>
  <form #form="ngForm" (ngSubmit)="addNewPerson(person)">
    <mat-form-field>
       <input type="text" [(ngModel)]="person.firstName" name="firstName"
                       placeholder="First Name" matInput required>
    </mat-form-field>
  </form>
</modal>

如何在通用模式组件中发送此表单中的数据?非常感谢

在这种情况下,使用 @Output() 将数据从子模式发送到通用模式不起作用。也很高兴看到如何从通用模式在组件中向下发送数据,就像我们可以在简单的组件层次结构中使用 @Input() 一样。

【问题讨论】:

    标签: javascript angular


    【解决方案1】:

    您可以添加一个自定义指令来获取对NgForm 指令的引用

    <form myFormDirective #form="ngForm" (ngSubmit)="addNewPerson(person)">
    
    @Directive({
      selector: '[myFormDirective]',
    })
    export class MyFormDirective {
      constructor(form:NgForm, myService:MyService) {
        console.log(form.value);
        myService.theForm = form;
      }
    }
    

    然后您可以使用共享服务来传递NgForm 引用,并根据需要从theForm.value 读取表单值或启动一些其他操作。

    【讨论】:

    • 我需要知道内容的具体添加位置。模态内容通常被添加到靠近 body 标签的地方。我不知道他使用的组件。但在其他方面值得一试。
    • 我不明白他想从哪个事件中获取数据,但我在他的通用模式中看到了button
    • 我猜你会在大约 2 分钟内想出一个堆栈闪电战并演示如何正确地做到这一点:D
    • 不,我没有足够的时间:)
    • 很高兴听到,所以我还有机会获得一些代表 ;-)。你在做什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    相关资源
    最近更新 更多