模态 - 无法接近并且@Input与外部组件一起工作

时间:2016-09-23 12:59:32

标签: angular ng-bootstrap

我尝试将@Input用于组件,但是当我点击打开模态时无法弄清楚如何发送变量。例如,我有以下模板:

<template #modalContent>
    <my-component-with-content [var1]="val1"></my-component-with-content>
</template> 

当我点击打开模态时:

<button type="button" (click)="open(modalContent)" class="btn btn-default">Open</button>

我也对关闭功能感到困惑。

我试过了:

<template #modalContent let-close='close'>
    <my-component-with-content></my-component-with-content>
</template>
当我尝试调用(click) = close("close")时,

和my-component-with-content(html)中出现以下错误:self.context.close不是函数

所以我的问题是如何在单击打开按钮时传递var1,如何将close函数传递给外部组件?

编辑 :我正在使用ng-bootstrap modal

1 个答案:

答案 0 :(得分:1)

注意,这是在Angular 2.0.1,ng-bootstrap alpha6

中实现的

您可以使用以下命令将close函数传递给组件:

<template #modalContent let-c="close"> <my-component [var1]="val1" [close]="c"></my-component> </template>

这使您可以调用绑定到modalContent的close函数。您为var1指定的输入绑定意味着您的输入是从父组件中名为val1的变量设置的,因此不需要在打开时传递,因为您列出的第一个方法应该有效。

import { Component, Input } from "@angular/core";

@Component({
selector: "my-component",
template: "<h2>{{var1}}</h2>" +
    "<button (click)='close()'>Close</button>"
})
export class MyComponent {
    @Input()
    var1: string;

    @Input()
    close: Function;
}

在我的包含组件声明中公开 val1: string = "some thing";

当我点击按钮打开时,它会显示某些东西,下面会有一个按钮,按下该按钮会关闭模态模板。