Angular 2 ng-bootstrap模式:如何将数据传递给入口组件

时间:2016-10-14 16:35:00

标签: javascript angular typescript ng-bootstrap

我尝试将数据发送到自定义模态内容组件,因此我可以从任何其他组件调用它而不重复代码。我是Angular 2的新手,并且已经跟随"组件作为内容" ng-boostrap的演示以及"组件交互"在Angular文档中并没有找到一种方法来使这个工作或这个案例的例子。

我可以打开模态,但不能使用动态内容。我尝试了@Input和变量方法但没有成功。我还在ModalService中向提供商添加了app.module.ts。这就是我用两种方法都行不通的方法:

page.component.html:

<a (click)="modal('message')">
<template ngbModalContainer><my-modal [data]="data"></my-modal></template>

page.component.ts:

import { Component } from '@angular/core'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { ModalService } from '../helpers/modal.service'
import { ModalComponent } from '../helpers/modal.component'

@Component({
  selector: 'app-page',
  templateUrl: './page.component.html',
  styleUrls: ['./page.component.scss'],
  entryComponents: [ ModalComponent ]
})

export class PageComponent {
  data = 'customData'
  constructor (
    private ngbModalService: NgbModal,
    private modalService: ModalService
   ) { }

  closeResult: string
  modal(content) {
    this.data = 'changedData'
    this.modalService.newModal(content)
    this.ngbModalService.open(ModalComponent).result.then((result) => {
      this.closeResult = `Closed with: ${result}`
    }, (reason) => {
      this.closeResult = `Dismissed ${reason}`
    });
  }
}

modal.service.ts:

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class ModalService {
  private modalSource = new Subject<string>()
  modal$ = this.modalSource.asObservable()
  newModal(content: string) {
    this.modalSource.next(content)
  }
}

modal.component.ts:

import { Component, Input, OnDestroy } from '@angular/core';
import { Subscription }   from 'rxjs/Subscription';
import { ModalService } from './modal.service'

@Component({
  selector: 'my-modal',
  template: `
    <div class="modal-body">
    {{data}}
    {{content}}
    </div>
  `
})

export class ModalComponent implements OnDestroy {
  @Input() data: string
  content = 'hello'

  subscription: Subscription
  constructor(
    private modalService: ModalService
  ) {
    this.subscription = modalService.modal$.subscribe(
      content => {
        this.content = content
    });
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

使用angular v2.1.0,angular-cli v1.0.0-beta.16,ng-bootstrap v1.0.0-alpha.8

3 个答案:

答案 0 :(得分:6)

我解决了!以下是如何做到这一点:

  • 在组件中(从您打开模态的位置)执行以下操作:

    const modalRef = this.modalService.open(ModalComponent);
    
    modalRef.componentInstance.passedData= this.dataToPass;
    
    modalRef.result.then(result => {
      //do something with result
    }                                                       
    
  • 在模态组件(接收端)中:

    export class ModalComponent { 
    
     passedData: typeOfData;     // declare it!
    
     someFunction() {     // or some  lifecycle hook 
      console.log(this.passedData)  // and then, use it!
     }
    
    // rest of the ModalComponent 
    }
    

答案 1 :(得分:2)

只需提供服务并将其注入VideoModalComponentPageComponent,然后您就可以使用此服务进行通信。

有关详细信息和示例,请参阅https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

答案 2 :(得分:0)

以下是一些方法。

  1. 公开RewriteEngine on RewriteCond %{REMOTE_ADDR} !**.**.**.* RewriteRule !^maintenance/ http://www.website.co.uk/maintenance/ [R=302,L]
  2. 的实例
  3. 使用resolve并在component
  4. 的实例上公开已解析的值
  5. 使用resolve并将已解析的值绑定到组件的输入。
  6. 请参阅:https://github.com/ng-bootstrap/ng-bootstrap/issues/861#issuecomment-253500089