【发布时间】:2018-08-02 20:38:42
【问题描述】:
我正在使用 Angular 5,虽然我不知道我的问题是否与版本或 Angular 或我编写组件的方式有关。
我正在创建一个外观和行为类似于表格的组件。它由两个主要部分组成:工具栏和表格本身。这是我的模板的简化示例:
<div class="list__table">
<!-- The toolbar -->
<div class="list-toolbar__wrapper" *ngIf="hasToolbar">
<div class="list-toolbar">
<div class="list-toolbar__right-column">
<div class="list-toolbar__right-column__conditional">
<toolbar-root [settings]="getToolbarItemsA()" i18nStrings="" [svgIconsPath]="svgIconsPath()"></toolbar-root>
</div>
<div class="list-toolbar__right-column__action">
<toolbar-root [settings]="getToolbarItemsB()" i18nStrings="" [svgIconsPath]="svgIconsPath()"></toolbar-root>
</div>
</div>
</div>
</div>
<!-- end of toolbar -->
<!-- Begining of content -->
<div>
<!-- Table head -->
<div
class="list__table__header"
[style.padding-right]="hasScrollbar ? scrollbarWidth : 0">
<div class="list-row list-row--head">
<span
class="list-checkbox list-row__checkbox"
[ngClass]="{'list-checkbox--checked': areAllRowsSelected, 'list-checkbox--indeterminate': areSomeRowsSelected}"
*ngIf="hasToolbar"
(click)="toggleAllChecked()">
<span class="list-checkbox__box"></span>
<svg class="list-checkbox__checked-mark">
<use xlink:href="#NotificationValidation" />
</svg>
<svg class="list-checkbox__indeterminate-mark">
<use xlink:href="#IndeterminateCheckbox" />
</svg>
</span>
<span
*ngFor="let column of getVisibleColumns()"
class="list-row__data list-row__data--{{column.size}}"
(click)="changeSelectedColumnSortingState(column.prop)">
<div class="list-row__data__text" [ngClass]="{'list-row__data__text--selected' : column.prop == selectedColumn}">
{{column.name}}
</div>
</span>
</div>
</div>
<!-- end of table head -->
<!-- Content rows -->
<div class="list__table__content" id="scrollableTableContent">
<!-- Repeatable rows -->
<div
class="list-row"
[ngClass]="{'list-row--selected': row.isSelected, 'list-row--selectable' : hasToolbar}"
*ngFor="let row of visibleRows | slice:0:20; index as i"
(click)="toggleSelectedRow(i)">
<span
class="list-checkbox list-row__checkbox"
*ngIf="hasToolbar"
[ngClass]="{'list-checkbox--checked': row.isSelected}">
<span class="list-checkbox__box"></span>
<svg class="list-checkbox__checked-mark">
<use xlink:href="#NotificationValidation" />
</svg>
</span>
<span
class="list-row__data list-row__data--{{column.size}}"
*ngFor="let column of getVisibleColumns()">
<div class="list-row__data__text" *ngIf="row[column.prop]">{{row[column.prop]}}</div>
</span>
</div>
<!-- end of repeatable rows -->
</div>
<!-- end of content rows -->
</div>
<!-- end of content -->
</div>
如您所见,我在list-toolbar__wrapper 上使用*ngIf 指令来控制工具栏在DOM 中的存在。 hasToolbar 在我的组件类中设置为 true,如下所示:public hasToolbar: boolean = true
但是,不管hasToolbar 的值如何,我在模板上使用*ngIf 的那一刻,我得到了一些奇怪的视觉“故障”。我在表头上的list-row__data 正在“闪烁”工具栏上的任何操作(例如悬停、单击)、它们本身和我的内容行。深入研究问题,我发现是什么触发了这种行为,但我不明白为什么会这样。
toolbar-root 组件本身有一个toolbar-item 组件,它最终包含了我的ripple-root 组件,它的模板如下所示:
<div #ripple class="entity-header-ripple" (click)="handleClick()" (mousedown)="handleMouseDown($event)" [style.borderRadius]="getBorderRadius()" [ngClass]="{'entity-header-ripple--clicked': isClicked}">
<div class="entity-header-ripple__circle" [style.backgroundColor]="color" [style.width.px]="getCircleDiameter()" [style.height.px]="getCircleDiameter()" [style.top.px]="getTopPosition()" [style.left.px]="getLeftPosition()" [ngStyle]="{'transformorigin': 2, '-webkit-transform-origin': 2}"></div>
</div>
当我用 [style] 或 [ngStyle] 更改分配给方法(getBorderRadius()、getCircleDiameter()、getTopPosition()、getLeftPosition())的每一位代码,并给它们静态值(如 ' 20'),不再有闪烁问题。
我需要我的工具栏是有条件的,我希望我的波纹组件保持原样或稍作修改。总的来说,我想了解导致问题的原因以便解决它。
【问题讨论】:
标签: angular angular5 angular-ng-if ng-style