将模板添加为innerHTML 时,Angular 2 绑定/事件不起作用

IT技术 javascript angular angular2-template
2021-03-16 09:36:50

我正在尝试创建一个可以配置的可重用表。单元格可以配置为具有 html 模板。

我正在配置“ Review列,使其具有带有单击事件(review(row))的锚标记的 html 模板。

到目前为止,我尝试将此模板作为innerHTML 插入,但是没有任何角度绑定起作用。然后我尝试创建一个动态调用的组件(review.component.ts),但该组件仅在第一行调用一次。此外,我没有找到任何方法将当前行项目传递给单击事件评论(行)(在 review.component.ts 内)。

员工.component.ts:

import { Component, OnInit, DynamicComponentLoader, ElementRef, Inject, Injector} from '@angular/core';
import { CORE_DIRECTIVES } from '@angular/common';
import {Router, RouteConfig, RouterLink, RouterOutlet, ROUTER_PROVIDERS, ROUTER_DIRECTIVES} from '@angular/router-deprecated';
import {DataTableComponent} from '../../data_table/data_table.component';
import {DataTable} from '../../data_table/dataTable';
import {Cell} from '../../data_table/cell';
import {ReviewComponent} from './review.component';

@Component({
    selector: 'employee',
    templateUrl: './app/employee/employee.html',
    directives: [DataTableComponent, ReviewComponent, RouterLink, RouterOutlet, ROUTER_DIRECTIVES],
    providers: [ROUTER_PROVIDERS]
})

export class EmployeeComponent implements OnInit {
    public tableConfig: DataTable;
    public constructor(private dcl: DynamicComponentLoader, private elementRef: ElementRef, private injector: Injector) {
        this.tableConfig = new DataTable();
        this.tableConfig.rows = [{ Sr: 1, Name: 'saurabh' }, { Sr: 2, Name: 'arun' }];
        var cellSr = new Cell();
        var cellName = new Cell();
        var cellReview = new Cell();
        cellSr.name = 'Sr';
        cellName.name = 'Name';

        //setting innerHtml directly doesn't work. Any way to make the bindings work in such case?
        //cellReview.innerHtml='<a class="btn btn-default" (click)="review(row)">Review</a>'

        cellReview.innerHtml = '<div class="child"></div>';
        this.tableConfig.cells = [cellSr, cellName, cellReview];
    }
    ngOnInit() {
        this.dcl.loadAsRoot(ReviewComponent, '.child', this.injector);
    }
}

review.component.ts:

import { Component} from '@angular/core';
import { CORE_DIRECTIVES } from '@angular/common';
import {Router, RouteConfig, RouterLink, RouterOutlet, ROUTER_PROVIDERS, ROUTER_DIRECTIVES} from '@angular/router-deprecated';

@Component({
    selector: 'btn-review',
    template: `<a class="btn btn-default" (click)="review(row)">Review</a>`,
    directives: [RouterLink, RouterOutlet, ROUTER_DIRECTIVES],
    providers: [ROUTER_PROVIDERS]
})

export class ReviewComponent {
    constructor() {}
    public review(event){
        console.log(event);
        console.log(2);
    }
 } 

数据表.ts:

import {Cell} from './cell';
export class DataTable {
    rows: Array<any>;
    cells: Cell[];
}

data_table.component.ts:

import { Component, Input } from '@angular/core';
import { CORE_DIRECTIVES } from '@angular/common';
import {Router, RouteConfig, RouterLink, RouterOutlet, ROUTER_PROVIDERS, ROUTER_DIRECTIVES} from '@angular/router-deprecated';
import {TAB_DIRECTIVES} from 'ng2-bootstrap';
import {DataTable} from './dataTable';
import {Cell} from './cell';
@Component({
    selector: 'data-table',
    templateUrl: './data_table/data_table.html',
    directives: [RouterLink, RouterOutlet, ROUTER_DIRECTIVES],
    providers: [ROUTER_PROVIDERS]
})
export class DataTableComponent {
    @Input() dataTable: DataTable;   
    bindCell(cell: Cell, row: any) {
        var text = '';
        if (cell.innerHtml && cell.innerHtml.length > 0) {
            return cell.innerHtml;
        }
    }   
}

数据表.html:

<table>
    <tbody>
        <tr *ngFor="let row of dataTable.rows">
            <td *ngFor="let cell of dataTable.cells" [innerHTML]="bindCell(cell, row)">
            </td>
        </tr>
    </tbody>
</table>
1个回答

Angular 不会以任何方式处理使用innerHtml(也 ElementRef.nativeElement.append(...)或类似)添加的 HTML

如评论中所述,另一种方法是将 HTML 包装在组件中并动态添加此组件。DynamicComponentLoader(如对您的问题的评论的链接答案所示)已弃用。它被替换了ViewContainerRef.createComponent()带有用户单击所选组件的 Angular 2 动态选项卡显示了如何使用它的示例。