- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Angular Material 设计组件并想创建一个自定义网格列表组件。该组件将根据其大小调整网格列表列的数量。组件模板如下所示:
<mat-grid-list [cols]="adaptiveCols">
<ng-content></ng-content>
</mat-grid-list>
组件将这样使用:
<my-grid>
<mat-grid-tile>tile_content</mat-grid-tile>
<mat-grid-tile>tile_content</mat-grid-tile>
<mat-grid-tile>tile_content</mat-grid-tile>
</my-grid>
所以你会认为这会显示一个 <mat-grid-list>
与 3 <mat-grid-tile>
s 在其中,生成的 DOM 将包含这些元素。但是您看不到图 block 及其内容。
向网格列表添加更多图 block 并使用固定 rowHeight
不影响 height
- 网格列表的属性(据我所知,网格列表根据图 block 的数量和 col-/rowspan 计算其高度)。这让你认为网格列表没有“看到” <mat-grid-tile>
- children 。看着 source code of the grid-list并在 _layoutTiles()
上设置断点在这种方法中证实了这一点 this._tiles
是空的。
我的猜测是 Angular ContentChildren
-annotation ( here ) 在第一个代码片段中找不到 tile-children 因为它只适用于 DOM 的第一层,而第一层我们只有 <ng-content>
.
想要的结果很明确,我希望能够使用自定义的网格列表组件,给它一些 child 并使用它的功能。但出于某种原因,angular 不希望我成功,或者我还有东西要学。
我正在使用 angular 6.1 并使用 official docs 安装了 Material 组件.
最佳答案
mat-grid-list 在它自己的实现中使用内容投影,因此它需要能够查询它的投影内容。尝试添加另一个级别的内容投影会使这成为不可能,因为 mat-grid-tile 实际上不是由列表投影的,而是由您的组件投影的。
如此简短的回答:这行不通。
可能有一种方法可以实现您的目标,但内容投影不是这种方法。如果你稍微澄清一下你的意图,我也许能帮上忙。不过,您可能需要使用模板。
这是一个简单的例子:围绕 mat-grid-list 编写一个包装器,它接受一个数据数组作为输入,您可以使用 ngFor 迭代它来创建图 block
@Component({
selector: 'my-grid',
templateUrl: './my-grid.html'
})
export class MyGrid {
@Input() data: string[];
}
<mat-grid-list>
<mat-grid-tile *ngFor="let d of data">{{d}}</mat-grid-tile>
</mat-grid-list>
这为 mat-grid-list 提供了一个可重用的包装器,它为您抽象了一些。但是,如果您的网格内容比字符串(可能)更复杂或每次使用都需要自定义模板,这就不好了。在这种情况下,您需要一种更精细的方法,使用模板和指令:
@Directive({
selector: 'gridTemplate'
})
export class GridTemplate {
@Input() key: string;
constructor(private elementRef: ElementRef, public template: TemplateRef<any>) {}
}
此指令将识别您的自定义模板,然后在您的网格列表中,您可以使用这些模板来填充图 block
@Component({
selector: 'my-grid',
templateUrl: './my-grid.html'
})
export class MyGrid implements AfterContentInit {
//structure the data so it can be assigned to a template by key
@Input() data: {templateKey:string, data: any}[];
//use content children to find projected templates
@ContentChildren(GridTemplate)
gridTemplates: QueryList<GridTemplate>;
gridTemplateMap: {[key:string]: TemplateRef<any>} = {};
ngAfterContentInit() {
//store queried templates in map for use in template
this.gridTemplates.forEach(t => this.gridTemplateMap[t.key] = t.template);
}
}
<ng-template #defaultGridTemp let-d="data">{{d}}</ng-template>
<mat-grid-list>
<mat-grid-tile *ngFor="let d of data">
<!-- here we use the template map to place the correct templates or use a default -->
<!-- we also feed the data property as context into the template -->
<ng-container [ngTemplateOutlet]="(gridTemplateMap[d.templateKey] || defaultGridTemp)"
[ngTemplateOutletContext]="{ data: d.data }" ></ng-container>
</mat-grid-tile>
</mat-grid-list>
那么你的组件用法是这样的:
@Component({...})
export class MyComponent {
data = [
{ templateKey:'MyKey1', data: {message: 'Projected Message', number: 1} },
{ templateKey:'MyKey2', data: {whatever: 'Message', youWant: 'to place'} }
];
}
<my-grid [data]="data">
<ng-template gridTemplate key="MyKey1" let-d="data">
<div>{{d.message}}</div>
<div>{{d.number}}</div>
<div>Any other arbitrary content</div>
</ng-template>
<ng-template gridTemplate key="MyKey2" let-d="data">
<div>{{d.whatever}}</div>
<div>{{d.youWant}}</div>
<div>Any other arbitrary content</div>
</ng-template>
</my-grid>
通过这种方法,您可以在网格组件内将任何可重用逻辑添加到您想要的 mat-grid-list,但仍保持为网格提供自定义模板的灵 active 。缺点是您必须采取额外的步骤来构建数据才能利用这种方法。
关于javascript - Angular - mat-grid-list 不显示 <ng-content> 传递的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51801295/
我想使用 R 预定义这样的列表 DATA<-list( list(list(),list(),list()), list(list(),list(),list()), list(list(),l
如何将一个列表添加到另一个列表,返回一个列表的列表? foo :: [a] -> [a] -> [[a]] 例如,我想要的结果是: foo [1,2] [3,4] 将是 [[1,2], [3,4]]。
我还没有在这里找到类似问题的解决方案,所以我会寻求你的帮助。 有 2 个列表,其中之一是列表列表: categories = ['APPLE', 'ORANGE', 'BANANA'] test_re
这个问题不同于Converting list of lists / nested lists to list of lists without nesting (这会产生一组非常具体的响应,但无法解决
原始列表转换为 List正好。为什么原始列表的列表不能转换为 List 的列表? { // works List raw = null; List wild = raw; } {
在下面的代码中,get()被调用并将其结果分配给类型为 List> 的变量. get()返回 List>并在类型参数为 T 的实例上调用设置为 ? ,所以它应该适合。 import java.util
原始列表转换为 List正好。为什么原始列表的列表不能转换为 List 的列表? { // works List raw = null; List wild = raw; } {
在insufficiently-polymorphic 作者说: def foo[A](fst: List[A], snd: List[A]): List[A] There are fewer way
我有下面的代码有效。 class ListManipulate(val list: List, val blockCount: Int) { val result: MutableList>
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
在 scala (2.9) 中转换列表列表的最佳方法是什么? 我有一个 list : List[List[A]] 我想转换成 List[A] 如何递归地实现这一点?或者还有其他更好的办法吗? 最佳答案
我编写了这个函数来确定给定元素是否存储在元组列表的列表中,但目前它只搜索第一个列表。我将如何搜索其余列表? fun findItem (name : command, ((x,y)::firstlis
我创建了一个类名 objectA,它有 4 个变量:约会时间;字符串文本;变量 1,变量 2 我需要创建一个 ObjectA() 列表。然后首先按时间对它们进行分组,其次按 var1,然后按 var2
我有一套说法 char={'J','A'} 和列表的列表 content = [[1,'J', 2], [2, 'K', 3], [2, 'A', 3], [3,'A', 9], [5, 'J', 9
我有以下列表 List >>> titles = new ArrayList >>> ();我想访问它的元素,但我不知道该怎么做.. 该列表有 1 个元素,它又包含 3 个元素,这 3 个元素中的
转换 List[List[Long]] 的最佳方法是什么?到 List[List[Int]]在斯卡拉? 例如,给定以下类型列表 List[List[Long]] val l: List[List[Lo
我有一个来自 Filereader (String) 的 List-List,如何将其转换为 List-List (Double):我必须返回一个包含 line-Array 的第一个 Values 的
我收集了List> 。我需要将其转换为List> 。这是我尝试过的, List> dataOne = GetDataOne(); var dataTwo = dataOne.Select(x => x
这个问题在这里已经有了答案: Cannot convert from List to List> (3 个答案) 关闭 7 年前。 我没有得到这段代码以任何方式编译: List a = new Ar
这个问题在这里已经有了答案: Cannot convert from List to List> (3 个答案) 关闭 7 年前。 我没有得到这段代码以任何方式编译: List a = new Ar
我是一名优秀的程序员,十分优秀!