打开包含从另一个 ngx-formly 表单创建的表单的模态表单 [英] Open modal form containing form created from ngx-formly from another ngx-formly form

查看:25
本文介绍了打开包含从另一个 ngx-formly 表单创建的表单的模态表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 ngx-formly 从 JSON 动态创建一堆 Angular 表单,效果非常好.我有一个特殊的用例,表单上的自定义按钮应该打开一个包含另一个表单的模式对话框,其中还包含一个使用 ngx-formly 创建的表单.我在 ngx-formly 站点上看到的示例使用自定义按钮,并使用 .ts 文件创建自定义组件,但我想避免这种情况,因为我将有多个表单执行此操作,而且我不想创建不同的组件为此.

I'm currently using ngx-formly to dynamically create a bunch of Angular forms from JSON, which works really nicely. I have a peculiar use case where a custom button on a form, should open a modal dialog containing another form on click, which would also contain a form created using ngx-formly. The example I saw on the ngx-formly site use a custom button, and creates a custom component with .ts files, but I want to avoid that since I would have several forms doing this, and I don't want to create different components for this.

有没有办法从 ngx-formly 表单触发模态对话框,以显示具有 ngx-formly 表单的模态,而无需为它们创建多个组件(.ts)文件?

Is there a way to trigger a modal dialog from an ngx-formly form, to show the modal with ngx-formly form without having to create multiple components(.ts) files for them?

推荐答案

Common Bootstrap Model with dynamic data

modal.service.ts

import {Injectable} from '@angular/core';
import {ModalModel} from './modal.model';
import {Subject} from "rxjs/Subject";

declare let $: any;

@Injectable()
export class ModalService {

  modalData = new Subject<ModalModel>();

  modalDataEvent = this.modalData.asObservable();

  open(modalData: ModalModel) {

    this.modalData.next(modalData);

    $('#myModal').modal('show');
  }

}

modal.component.ts

import { Component } from '@angular/core';
import { ModalService } from './modal.service';
import {ModalModel} from './modal.model';

declare let $: any;

@Component({
  selector: 'app-modal',
  templateUrl: './modal.component.html',
  styleUrls: [ './modal.component.css' ]
})
export class ModalComponent  {

  modalData: ModalModel;

  constructor(private modalService: ModalService) {
    this.modalService.modalDataEvent.subscribe((data) => {
      this.modalData = data;
    })
  }

}

从任何组件调用此服务

import { Component } from '@angular/core';
import { ModalService } from '../modal/modal.service';
import { ModalModel } from '../modal/modal.model';


declare let $: any;

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: [ './home.component.css' ]
})
export class HomeComponent  {

  modelData = new ModalModel();

  constructor(private modalService: ModalService) {

  }

  open() {
    this.modelData.header = 'This is my dynamic HEADER from Home component';
    this.modelData.body = 'This is my dynamic BODY from Home component';
    this.modelData.footer = 'This is my dynamic footer from Home component';
    this.modalService.open(this.modelData);
  }
}

  • 不使用 jQuery 的示例,即使用 ngx-bootstrap: https://stackblitz.com/edit/angular-ngx-bootstrap-modal
  • 这篇关于打开包含从另一个 ngx-formly 表单创建的表单的模态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆