[英]Angular directive selector can't apply icon to the element
I'm trying to do something similar to this application:我正在尝试做类似于此应用程序的事情:
https://stackblitz.com/edit/angular-table-sort-and-filter?file=src%2Fapp%2Fapp.component.ts https://stackblitz.com/edit/angular-table-sort-and-filter?file=src%2Fapp%2Fapp.component.ts
This is my application:这是我的申请:
https://stackblitz.com/edit/angular-table-sort-and-filter-w9hrc3?file=src%2Fapp%2Fapp.component.html https://stackblitz.com/edit/angular-table-sort-and-filter-w9hrc3?file=src%2Fapp%2Fapp.component.html
HTML: HTML:
<thead class="thead-light">
<tr>
<th
*ngFor="let item of list.table.headings; let i = index"
scope="col"
sortable="{{ item.content }}"
(sort)="onSort($event, i)"
>
{{ item.content }}
</th>
</tr>
</thead>
Directive:指示:
export type SortColumn = string | '';
export type SortDirection = 'asc' | 'desc' | '';
const rotate: { [key: string]: SortDirection } = {
asc: 'desc',
desc: '',
'': 'asc',
};
export const compare = (
v1: string | number | boolean | Date,
v2: string | number | boolean | Date
) => (v1 < v2 ? -1 : v1 > v2 ? 1 : 0);
export interface SortEvent {
column: SortColumn;
direction: SortDirection;
}
@Directive({
selector: 'th[sortable]',
host: {
'[class.asc]': 'direction === "asc"',
'[class.desc]': 'direction === "desc"',
'(click)': 'rotate()',
},
})
export class SortableHeaderDirective {
@Input() sortable: SortColumn = '';
@Input() direction: SortDirection = '';
@Output() sort = new EventEmitter<SortEvent>();
rotate() {
this.direction = rotate[this.direction];
this.sort.emit({ column: this.sortable, direction: this.direction });
}
}
An up and a down icon is appearing when the user clicks on the header of the table in the original code (first link).当用户单击原始代码(第一个链接)中表的 header 时,会出现向上和向下图标。 But in my code, it didn't appear.
但是在我的代码中,它没有出现。 I don't know what I'm doing wrong.
我不知道我做错了什么。
If you inspect the rendered <th>
element:如果您检查呈现的
<th>
元素:
<th _ngcontent-hpt-c62="" scope="col" ng-reflect-sortable="Tercih"> Tercih </th>
There is no sortable
attribute, while your SortableHeaderDirective
is applied to <th>
element with sortable
attribute: 'th[sortable]'
.没有
sortable
属性,而您的SortableHeaderDirective
应用于具有sortable
属性的<th>
元素: 'th[sortable]'
。
With sortable="{{ item.content }}"
or [sortable]="item.content"
is used as property binding.使用
sortable="{{ item.content }}"
或[sortable]="item.content"
用作属性绑定。
You can perform either of these to add the sortable
attribute:您可以执行以下任一操作来添加
sortable
属性:
Add sortable
without string interpolation.添加无字符串插值的
sortable
。
Add [attr.sortable]="item.content"
.添加
[attr.sortable]="item.content"
。
<th
*ngFor="let item of list.table.headings; let i = index"
scope="col"
sortable="{{ item.content }}"
(sort)="onSort($event, i)"
sortable
>
{{ item.content }}
</th>
HTML: HTML:
<div class="row">
<div
*ngFor="let item of this.list.table.headings; let i = index"
class="table-view__item__col {{ item.class }}"
>
<div scope="col" (sort)="onSort($event, i, this.table)" sortable="{{ item.content }}">
{{ item.content }}
</div>
</div>
</div>
Directive:指示:
@Directive({
selector: 'div[sortable]',
host: {
'[class.sorting]': 'true',
'[class.sorting-desc]': 'direction === "asc"',
'[class.sorting-asc]': 'direction === "desc"',
'(click)': 'rotate()',
},
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.