【问题标题】:How to get Input values from nested component modal ngx-bootstrap using angular如何使用角度从嵌套组件模式 ngx-bootstrap 获取输入值
【发布时间】:2018-01-09 18:27:58
【问题描述】:

我正在使用 ngx-bootstrp Modals。我必须使用嵌套的service-component 模态来满足特定要求。我已经制作了现场演示来展示问题。

Demo

我希望(例如)全名出现在模态中,名字来自第一个模态,姓氏来自第二个模态,并作为用户输入进行更新。

import { Component, TemplateRef } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';


@Component({
    selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent{
  modalRef: BsModalRef;
  constructor(private modalService: BsModalService) {}
  person:any={}
  openModal(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template);
  }

  openModalWithComponent() {

    this.modalRef = this.modalService.show(ModalContentComponent);
    this.modalRef.content.title = 'Modal with component';

  }
}

/* This is a component which we pass in modal*/

@Component({
  selector: 'modal-content',
  template: `
    <div class="modal-header">
      <h4 class="modal-title pull-left">{{title}}</h4>
      <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
    <input type="text" placeholder="Last Name">

    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" (click)="modalRef.hide()">Close</button>
    </div>
  `
})
export class ModalContentComponent {
  title: string;
  constructor(private modalService: BsModalService) {}
}

我发现这个SO 帖子发现是相似的,它不是完全满足我的要求。

如何达到以上要求?

感谢您的宝贵时间..

【问题讨论】:

    标签: angular ngx-bootstrap


    【解决方案1】:

    您可以为两种模式使用共享对象。这不是一个完美的解决方案,但应该会有所帮助。

    第一个模态

    person = {fname: '', lname: ''};
    
    openModal() {
       this.modalRef = this.modalService.show(ModalContentComponent);
       this.modalRef.content.person = this.person;
    }
    

    第二个模态模板

    <input type="text" *ngIf="person" placeholder="Last Name" [(ngModel)]="person.lname">
    

    示例:https://stackblitz.com/edit/angular-nested-component-modal-ngx-bootstrap-not-working-k4pglr?file=app/app.component.ts

    【讨论】:

    • 另一个帮助解释为什么模态没有正确关闭。我也有问题,我在帖子中也没有提到
    • 看起来您对两个模态都使用相同的modalRef,因此当第二个模态打开时,您将覆盖对第一个模态的引用
    • 仍然没有运气检查这个更新的stackblitz.com/edit/…
    • 您忘记在第二个模态中注入 BsModalRef。示例 - stackblitz.com/edit/…
    • 谢谢,一切都好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2018-11-25
    相关资源
    最近更新 更多