如何为匹配给定值的元素设置 id?
How to set the id for element that matches a given value?
我在 Angular 7 中有以下内容:
<th *ngFor="let day of weekDays"><div>{{day}}</div></th>
但想将 corresponds/equals 的所有 weekDays
中的单个元素设置为 <th id="today"
到 today
。
鉴于:
this.today = "Fri 5/7";
结果应该是:
<th id="today"><div>Fri 5/7</div></th>
<th><div>Sat 6/7</div></th>
<th><div>Sun 7/7</div></th>
<th><div>Mon 8/7</div></th>
<th><div>Tue 9/7</div></th>
<th><div>Wed 10/7</div></th>
<th><div>Thu 11/7</div></th>
我该怎么做?
你可以这样做:
<th *ngFor="let day of weekDays" [id.today]="isToday()"><div>{{day}}</div></th>
当然,在您的打字稿中添加函数 isToday()
。
我通常使用条件 类,从未尝试过使用条件 ID,但这应该有效。
你应该调用一个根据日期设置 id 的函数:
<th *ngFor="let day of weekDays" [attr.id]="returnStringBasedOnCondition()"><div>{{day}}</div></th>
您可以使用以下链接获取 today
:
How to check if input date is equal to today's date?
您可以像这样有条件地添加属性。如果 null
它将删除您的属性:
<th *ngFor="let day of weekDays"
[attr.id]="value === day ? 'today' : null">
<div>{{day}}</div>
</th>
尝试这样的事情
<th *ngFor="let day of weekDays" [attr.id]="today==day?'today':null"><div>{{day}}</div></th>
我在 Angular 7 中有以下内容:
<th *ngFor="let day of weekDays"><div>{{day}}</div></th>
但想将 corresponds/equals 的所有 weekDays
中的单个元素设置为 <th id="today"
到 today
。
鉴于:
this.today = "Fri 5/7";
结果应该是:
<th id="today"><div>Fri 5/7</div></th>
<th><div>Sat 6/7</div></th>
<th><div>Sun 7/7</div></th>
<th><div>Mon 8/7</div></th>
<th><div>Tue 9/7</div></th>
<th><div>Wed 10/7</div></th>
<th><div>Thu 11/7</div></th>
我该怎么做?
你可以这样做:
<th *ngFor="let day of weekDays" [id.today]="isToday()"><div>{{day}}</div></th>
当然,在您的打字稿中添加函数 isToday()
。
我通常使用条件 类,从未尝试过使用条件 ID,但这应该有效。
你应该调用一个根据日期设置 id 的函数:
<th *ngFor="let day of weekDays" [attr.id]="returnStringBasedOnCondition()"><div>{{day}}</div></th>
您可以使用以下链接获取 today
:
How to check if input date is equal to today's date?
您可以像这样有条件地添加属性。如果 null
它将删除您的属性:
<th *ngFor="let day of weekDays"
[attr.id]="value === day ? 'today' : null">
<div>{{day}}</div>
</th>
尝试这样的事情
<th *ngFor="let day of weekDays" [attr.id]="today==day?'today':null"><div>{{day}}</div></th>