3 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
使用現(xiàn)有的表格元素作為選擇器
table元素不允許將<table-line>元素作為子元素,瀏覽器只會(huì)在找到它們時(shí)將其刪除。您可以將其包裝在組件中,并仍然使用允許的<tr>標(biāo)簽。只是"tr"用作選擇器。
使用 <template>
<template>也應(yīng)被允許,但并非在所有瀏覽器中都適用。Angular2實(shí)際上從不<template>向DOM 添加元素,而只是在內(nèi)部對(duì)其進(jìn)行處理,因此,它也可以在所有帶有Angular2的瀏覽器中使用。
屬性選擇器
另一種方法是使用屬性選擇器
@Component({
selector: '[my-tr]',
...
})
像
<tr my-tr>

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
我發(fā)現(xiàn)該示例非常有用,但在2,2.3版本中不起作用,因此經(jīng)過大量的頭部抓撓后,只需進(jìn)行一些小改動(dòng)即可使其再次起作用。
import {Component, Input} from '@angular/core'
@Component({
selector: "[my-tr]",
template: `<td *ngFor='let item of row'>{{item}}</td>`
})
export class MyTrComponent {
@Input("line") row:any;
}
@Component({
selector: "my-app",
template: `<h1>{{title}}</h1>
<table>
<tr *ngFor="let line of data" my-tr [line]="line"></tr>
</table>`
})
export class AppComponent {
title = "Angular 2 - tr attribute selector!";
data = [ [1,2,3], [11, 12, 13] ];
constructor() { console.clear(); }
}

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊
這是一個(gè)使用帶有屬性選擇器的組件的示例:
import {Component, Input} from '@angular/core';
@Component({
selector: '[myTr]',
template: `<td *ngFor="let item of row">{{item}}</td>`
})
export class MyTrComponent {
@Input('myTr') row;
}
@Component({
selector: 'my-app',
template: `{{title}}
<table>
<tr *ngFor="let line of data" [myTr]="line"></tr>
</table>
`
})
export class AppComponent {
title = "Angular 2 - tr attribute selector";
data = [ [1,2,3], [11, 12, 13] ];
}
輸出:
1 2 3
11 12 13
當(dāng)然,MyTrComponent中的模板會(huì)涉及更多,但是您可以理解。
舊的(beta.0)矮人。
- 3 回答
- 0 關(guān)注
- 840 瀏覽
添加回答
舉報(bào)