在php中,有可能以条件方式关闭html标签。我们能否在Angular 2中模拟类似的功能呢?
例如:
<p>
Some lorem
<?php if(someConditionTrue){ echo "</p>"}; ?>
在php中,有可能以条件方式关闭html标签。我们能否在Angular 2中模拟类似的功能呢?
例如:
<p>
Some lorem
<?php if(someConditionTrue){ echo "</p>"}; ?>
private items: string[] = ['test1','test2','test3','test4','test5','test6','test7','test8','test9'];
HTML:
<div *ngFor="let item of items; let i = index">
<div class="row" *ngIf="i%4==0">
<div *ngIf="items[i]">{{items[i]}}</div>
<div *ngIf="items[i+1]">{{items[i+1]}}</div>
<div *ngIf="items[i+2]">{{items[i+2]}}</div>
<div *ngIf="items[i+3]">{{items[i+3]}}</div>
</div>
</div>
https://plnkr.co/edit/RDZFJFEZtTD0NA5d8YNJ?p=preview
注意:最好创建一个数组(行),其中包含数组(项),而不是使用我发布的“仅模板”解决方案。
该链接是关于IT技术的内容,它展示了一个网页模板。建议使用数组来存储数据,以便更好地组织和管理信息。
<div class="container-fluid">
<div class="row justify-content-start">
<div class="col-12 col-lg-4">
<box1></box1>
<box1></box1>
<box1></box1>
</div>
<div class="col-12 col-lg-4">
<box1></box1>
<box1></box1>
<box1></box1>
</div>
<div class="col-12 col-lg-4">
<box1></box1>
<box1></box1>
<box1></box1>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row justify-content-start">
<div class="col-12 col-lg-4">
<ng-container *ngFor="let cat of categoryProducts; let i = index">
<box1 *ngIf="i%4 == 0">{{cat.name}}</box1>
<ng-container
</div>
<div class="col-12 col-lg-4">
<ng-container *ngFor="let cat of categoryProducts; let i = index">
<box1 *ngIf="i%4 == 1">{{cat.name}}</box1>
<ng-container
</div>
<div class="col-12 col-lg-4">
<ng-container *ngFor="let cat of categoryProducts; let i = index">
<box1 *ngIf="i%4 == 2">{{cat.name}}</box1>
<ng-container
</div>
</div>
</div>
因此,最终您的代码会相当冗余,但您可以通过以下方式解决问题:
<div class="iterable-container">
<div *ngFor="let item of items; let i = index">
<div class="iterable-col-1" *ngIf="(i+3) % 3 == 0">
<p>first column: {{item.name}}</p>
</div>
<div class="iterable-col-2" *ngIf="(i+3) % 3 == 1">
<p>second column: {{item.name}}</p>
</div>
<div class="iterable-col-3" *ngIf="(i+3) % 3 == 2">
<p>third column: {{item.name}}</p>
</div>
</div>
</div>
您还需要一些CSS。以下是一些起始样式:
.iterable-container {
background-color: #f2f2f2;
}
.iterable-col-1 {
display: flex;
height: 175px;
}
.iterable-col-2 {
display: flex;
height: 0;
left: 33%;
position: relative;
top: -175px;
}
.iterable-col-3 {
display: flex;
height: 0;
left: 66%;
position: relative;
top: -175px;
}
你为什么需要这个?这似乎是在与框架对抗而不是拥抱它。
有几种类似的方法,比如ng-if、ng-repeat和ng-show。
一种方法是:
<p>
<span ng-show='condition' ng-repeat='item in collection'>
{{item.Text}}
</span>
</p>
我的先前的回答假设你需要每列(由文本“first”、“second”、“third”表示)不同的内容。
Simon Aronsson 正确指出了这是违反框架的。如果你只需要一致风格的行和列内容,可以简化如下:
<div class="iterable-container">
<div *ngFor="let item of items">
<div class="iterable-col of-3">
<p>{{item.name}}</p>
</div>
</div>
</div>
我使用.of-2
和.of-3
类来将相似的架构应用于2列和3列视图。以下是一些起始Sass代码:
// assumes you have a color map:
.iterable-container {
background-color: color($colors, light);
}
.iterable-col {
display: flex;
position: relative;
float: left;
clear: right;
}
// first column
.iterable-col {
&.of-2:nth-of-type(2n+1){
width: 50%;
max-height: 300px; // depending on size of your card
}
&.of-3:nth-of-type(3n+1){
width: 33.33%;
}
}
// 2nd of 3 columns
.iterable-col.of-3:nth-of-type(3n+2){
width: 33.33%;
}
// last column
.iterable-col{
&.of-2:nth-of-type(2n){
width: 50%;
max-height: 300px;
}
&.of-3:nth-of-type(3n){
width: 33.33%;
}
}