Gre*_*reg 75 javascript angular
我正在努力找到一种方法来做到这一点.在父组件中,模板描述了a table及其thead元素,但委托将其呈现tbody给另一个组件,如下所示:
<table>
<thead>
<tr>
<th>Name</th>
<th>Time</th>
</tr>
</thead>
<tbody *ngFor="let entry of getEntries()">
<my-result [entry]="entry"></my-result>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
每个myResult组件都会呈现自己的tr标记,基本上是这样的:
<tr>
<td>{{ entry.name }}</td>
<td>{{ entry.time }}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我没有直接将它放在父组件中(避免需要myResult组件)的原因是myResult组件实际上比这里显示的更复杂,所以我想把它的行为放在一个单独的组件和文件中.
结果DOM看起来很糟糕.我相信这是因为它是无效的,因为tbody只能包含tr元素(参见MDN),但我生成的(简化的)DOM是:
<table>
<thead>
<tr>
<th>Name</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<my-result>
<tr>
<td>Bob</td>
<td>128</td>
</tr>
</my-result>
</tbody>
<tbody>
<my-result>
<tr>
<td>Lisa</td>
<td>333</td>
</tr>
</my-result>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
有没有什么方法我们可以得到相同的渲染,但没有包装<my-result>标签,并仍然使用组件独自负责渲染表行?
我已经看过了ng-content,DynamicComponentLoader的ViewContainerRef,但他们似乎并不至于我可以看到提供一个解决的办法.
Gün*_*uer 70
您可以使用属性选择器
@Component({
selector: '[myTd]'
...
})
Run Code Online (Sandbox Code Playgroud)
然后像使用它一样
<td myTd></td>
Run Code Online (Sandbox Code Playgroud)
rob*_*ing 23
你可以尝试使用新的 css display: contents
这是我的工具栏 scss:
:host {
display: contents;
}
:host-context(.is-mobile) .toolbar {
position: fixed;
/* Make sure the toolbar will stay on top of the content as it scrolls past. */
z-index: 2;
}
h1.app-name {
margin-left: 8px;
}
Run Code Online (Sandbox Code Playgroud)
和 html:
<mat-toolbar color="primary" class="toolbar">
<button mat-icon-button (click)="toggle.emit()">
<mat-icon>menu</mat-icon>
</button>
<img src="/assets/icons/favicon.png">
<h1 class="app-name">@robertking Dashboard</h1>
</mat-toolbar>
Run Code Online (Sandbox Code Playgroud)
并在使用中:
<navigation-toolbar (toggle)="snav.toggle()"></navigation-toolbar>
Run Code Online (Sandbox Code Playgroud)
Nic*_*ray 13
属性选择器是解决这个问题的最好方法。
所以在你的情况下:
<table>
<thead>
<tr>
<th>Name</th>
<th>Time</th>
</tr>
</thead>
<tbody my-results>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我的结果 ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-results, [my-results]',
templateUrl: './my-results.component.html',
styleUrls: ['./my-results.component.css']
})
export class MyResultsComponent implements OnInit {
entries: Array<any> = [
{ name: 'Entry One', time: '10:00'},
{ name: 'Entry Two', time: '10:05 '},
{ name: 'Entry Three', time: '10:10'},
];
constructor() { }
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
我的结果 html
<tr my-result [entry]="entry" *ngFor="let entry of entries"><tr>
Run Code Online (Sandbox Code Playgroud)
我的结果 ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: '[my-result]',
templateUrl: './my-result.component.html',
styleUrls: ['./my-result.component.css']
})
export class MyResultComponent implements OnInit {
@Input() entry: any;
constructor() { }
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
我的结果 html
<td>{{ entry.name }}</td>
<td>{{ entry.time }}</td>
Run Code Online (Sandbox Code Playgroud)
查看工作堆栈闪电战:https ://stackblitz.com/edit/angular-xbbegx
小智 11
在您的元素上使用此指令
@Directive({
selector: '[remove-wrapper]'
})
export class RemoveWrapperDirective {
constructor(private el: ElementRef) {
const parentElement = el.nativeElement.parentElement;
const element = el.nativeElement;
parentElement.removeChild(element);
parentElement.parentNode.insertBefore(element, parentElement.nextSibling);
parentElement.parentNode.removeChild(parentElement);
}
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
<div class="card" remove-wrapper>
This is my card component
</div>
Run Code Online (Sandbox Code Playgroud)
并在父html中照常调用card元素,例如:
<div class="cards-container">
<card></card>
</div>
Run Code Online (Sandbox Code Playgroud)
输出将是:
<div class="cards-container">
<div class="card" remove-wrapper>
This is my card component
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
小智 8
您需要“ ViewContainerRef”,并在my-result组件中执行以下操作:
的HTML:
<ng-template #template>
<tr>
<td>Lisa</td>
<td>333</td>
</tr>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
ts:
@ViewChild('template') template;
constructor(
private viewContainerRef: ViewContainerRef
) { }
ngOnInit() {
this.viewContainerRef.createEmbeddedView(this.template);
}
Run Code Online (Sandbox Code Playgroud)
现在的另一种选择是ContribNgHostModule从@angular-contrib/common包中提供。
导入模块后,您可以添加host: { ngNoHost: '' }到@Component装饰器中,并且不会呈现任何包装元素。
| 归档时间: |
|
| 查看次数: |
24300 次 |
| 最近记录: |