Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

javascript - Angular 2 binding/events not working when adding template as innerHTML

I am trying to create a reusable table which can be configurable. The cells can be configured to have html template.

I am configuring the column "Review" to have html template with anchor tag with click event (review(row)).

So far I tried to insert this template as innerHTML, but then none of the angular binding works. Then I tried to create a dynamically invoked component (review.component.ts) but this component invoked only once for first row. Moreover I didn't find any way to pass the current row item to click event review(row) (inside review.component.ts).

employee.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);
    }
 } 

datatable.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;
        }
    }   
}

data_table.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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Angular doesn't process HTML added using innerHtml (also ElementRef.nativeElement.append(...) or similar) in any way.

As mentioned in the comment an alternative is to wrap the HTML in a component and add this component dynamically. DynamicComponentLoader (as shown in the linked answer from the comment to your question) is deprecated. It was replaced by ViewContainerRef.createComponent(). Angular 2 dynamic tabs with user-click chosen components shows an example how to use it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...