【问题标题】:Angular2 - using child component as value of an attributeAngular2 - 使用子组件作为属性的值
【发布时间】:2016-02-28 14:01:28
【问题描述】:

我想在用户单击输入字段时显示一个弹出框,这可以正常工作,但我希望该弹出框的 data-content 属性来自子组件的模板。这是一个例子:

parent.ts

import {Component,AfterViewInit} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {ChildComponent} from './child_test.ts';


@Component({
    selector: 'app',
    template: `<input type='text' data-toggle="popover" data-trigger="focus" data-placement="bottom" [attr.data-content]="getPopoverContent()" />`,
    providers: [ChildComponent]
})
class AppComponent implements AfterViewInit{
    constructor(private _child: ChildComponent) {}

    getPopoverContent(){
        return this._child; //returning empty object instead of child template
    }
    ngAfterViewInit(){
        $("input").popover();
    }

}

bootstrap(AppComponent);

child.ts

import {Component} from 'angular2/core';

@Component({
    selector: "child-component",
    template: "<div>Popover content from child.</div>"
})
export class ChildComponent{};

我应该使用 DynamicComponentLoader 而不是依赖注入吗?如果是这样,那么我该如何实现呢?

【问题讨论】:

  • 为什么提供者中有组件?它应该在指令上,你应该查询它,而不是那样注入它。

标签: javascript angular


【解决方案1】:

这里有一个解决方法:

将临时变量分配给要显示的组件

<transaction-filter #popoverComponent></transaction-filter>

重要提示:上面的组件必须在其定义中公开一个 ElementRef

constructor(public el: ElementRef) {}

创建将显示弹出框的元素

<button class="btn-sm btn-link text-muted"
    data-animation="true"
    data-placement="bottom"
    title="Create Rule"
    [popover]="popoverComponent">

    Create Rule...
</button>

现在是 popover 指令本身:

/// <reference path="../../typings/tsd.d.ts"/>

import 'bootstrap'

import { Directive, ElementRef, Input} from 'angular2/core'

declare var $: JQueryStatic;

@Directive({
    selector: '[popover]',
})
export class PopoverDirective {
    @Input('popover') _component: any
    _popover: JQuery

    constructor(private _el: ElementRef) { }

    ngOnInit() {
        // Hide the component
        this._component.el.nativeElement.style.display = "none"

        // Attach it to the content option
        this._popover = $(this._el.nativeElement)
            .popover({
            html: true,
            content: this._component.el.nativeElement
        })

        // When the below event fires, the component will be made visible and will remain
        this._popover.on('shown.bs.popover', () => {
            this._component.el.nativeElement.style.display = "block"
        })
    }
}

【讨论】:

    【解决方案2】:

    一个问题是绑定到属性会字符串化值

     [attr.data-content]
    

    因此这种方法行不通。

    Bootstrap 弹出窗口似乎需要一个字符串,因此这很好,但是将 Angular 组件字符串化不会给它 HTML。

    【讨论】:

    • 所以你是说你无法将属性的值绑定到某个子模板?
    • 您需要将组件添加到 DOM,然后在 ngAfterViewInit() 中读取它的内部/外部 HTML。这似乎不是一个合适的解决方案。
    • 为什么它需要是一个组件。如果您首先将 HTML 作为字符串传递,它将开箱即用。
    • 可能有一种方法可以从组件实例中获取模板字符串,但您不能期望其中的任何 Angular 绑定都可以通过这种方式添加的 HTML 工作。
    • 它必须是一个组件,因为我想给它一些输入并将生成的模板用作数据绑定属性的值。
    猜你喜欢
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 2016-12-21
    相关资源
    最近更新 更多