【问题标题】:What's the correct way to load and open a component inside a modal dialog in Angular4 --在Angular4的模态对话框中加载和打开组件的正确方法是什么——
【发布时间】:2018-12-07 20:18:37
【问题描述】:

我有一个名为 NewCustomerComponent 的组件,我想在单击按钮时通过另一个页面/组件中的模式弹出窗口加载和显示它。我已经编写了相关的代码[或者看起来如此]。但我收到以下错误--

    this._childInstance.dialogInit is not a function
at ModalDialogComponent.dialogInit (modal-dialog.component.js:65)
at ModalDialogService.openDialog (modal-dialog.service.js:26)
at OrderNewComponent.newCl (order-new.component.ts:85)

我的代码也很简单,在我试图打开模式弹出窗口的组件中。 我只会发布相关部分--

    import { Component, Inject, ViewContainerRef, ComponentRef } from 
             '@angular/core';
    import { Http, Headers } from '@angular/http';
    import { Router } from '@angular/router';
    import { Observable, Subject } from 'rxjs';
    import 'rxjs/add/operator/map';
    import { CustomerSearchService } from '../../../shared/services/customer- 
       search.service';
    import { ICustomer, Customer, CustomerD } from 
       '../../../shared/models/customer';
    import { ModalDialogModule, ModalDialogService, IModalDialog } from 'ngx- 
      modal-dialog';
    import { NewCustomerComponent } from 
      '../../../components/popups/customer/customer-new.component';


    @Component({
        selector: 'order-new',
        templateUrl: './order-new.component.html' 
    })

    export class OrderNewComponent {

    public reference: ComponentRef<IModalDialog>;

    constructor(private cusService: CustomerSearchService, private http: 
        Http, private modalService: ModalDialogService, private viewRef: 
        ViewContainerRef) {

    }

   ngOnInit(): void {

   }


  ** this is where I am trying to load the newcustomercomponent and open it 
  in the popup. not working.     
  newCl() {
     this.newC = true;
     this.exiC = false;

     this.modalService.openDialog(this.viewRef, {
        title: 'Add New Customer',
        childComponent: NewCustomerComponent
     });
   }

 }

** 编辑。添加了 NewCustomerComponent 代码以供参考。

    import { Component, Input, Output, EventEmitter, OnInit, 
       ChangeDetectorRef, Directive, ElementRef, Renderer, AfterViewInit } 
       from '@angular/core';
    import { Router, ActivatedRoute } from '@angular/router';
    import { NgFor } from '@angular/common';
    import { Observable } from 'rxjs/Rx';
    import { BehaviorSubject } from 'rxjs/Rx';
    import { PlatformLocation } from '@angular/common';
    import { Http } from '@angular/http';
    import { ICustomer, Customer } from '../../../shared/models/customer';
    import { UserService } from '../../../shared/services/UserService';
    import { IModalDialog, IModalDialogOptions, IModalDialogButton } from 
         'ngx-modal-dialog';
    @Component({
       selector: 'new-customer',
       templateUrl: './customer-new.component.html'
    })

    export class NewCustomerComponent implements IModalDialog {
       model: Customer = new Customer();
       errors: any;
       submitResponse: any;
       actionButtons: IModalDialogButton[];

       constructor(private userService: UserService, private http: Http) {
                  this.actionButtons = [
                  { text: 'Close', onAction: () => true }
               ];
       }


      ngOnInit() {

      }

      dialogInit(reference: ComponentRef<IModalDialog>, options: 
                       Partial<IModalDialogOptions<any>>) 
      {
                // no processing needed
      }

     createCustomer() {
          this.userService.createCustomer(this.model)
        .take(1)
        .subscribe(
            (response: any) => {
                this.submitResponse = response;

                if (response.success) {
                    console.log('New customer added!');
                }
                else {
                    console.log('Unable to add customer!');
                }
            },
            (errors: any) => this.errors = errors
        );
    return false;
}

     cancelClicked() {

     }

   }

我在这里做错了什么?它与我根据 viewRef 添加的元素引用有关吗?哪个部分是错误的?那个子组件呢?是否需要一些特定的配置/标记/组件才能工作?我对角度很陌生;我不确定是什么原因。

请帮助我纠正这种情况。 提前致谢,

【问题讨论】:

标签: javascript angular modal-dialog elementref


【解决方案1】:

您能否确保NewCustomerComponent 正在实现IModalDialog 接口。另外,如果不是这种情况,请您也分享NewCustomerComponent的代码。

修改

看起来您还没有在NewCustomerComponent 中定义 dialogInit 方法,并且它之前没有弹出,因为您还没有实现接口IModalDialog。我会要求您按照link 的建议在组件类中定义 dialogInit 方法。

【讨论】:

  • 嗨@AshishKhandelwal,我已经添加了NewCustomerComponent 代码。请看一看。请注意——我也在其他地方以简单的正常方式展示了这个相同的 newcustomercomponent 组件,就像一个正常的页面一样。希望,添加 IModal 界面不会以任何方式阻碍这一点。只是确认一下,谢谢,
  • @PrabirChoudhury 我已经编辑了答案。看看吧。
  • 嗨@AshishKhandelwal 感谢您的链接。现在阅读 - 将尝试按照文档中的指南进行操作;如果有更多的并发症,我会回来的。
  • 嗨@AshishKhandelwal 现在只发生了1个问题-单击按钮后,模态显示,但仅显示一小部分,然后转义。请检查我已实现的 newcustomercomponent 代码的更新部分。为什么会发生?我为 onAction()=> 尝试了 true false 的值 => 不工作。
  • 不,我仍然坚持这一点。我做了几个改变;请看一下。 @AshishKhandelwal。对话框正在显示但几乎立即关闭。自动。没有显示控制台错误,因此我也看不到任何消息/指示。谁知道原因,请帮忙。
猜你喜欢
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 2020-10-24
  • 2012-04-15
  • 1970-01-01
相关资源
最近更新 更多