Angular如何实现主题而不是输入和模板变量

发布于 2025-02-11 17:32:35 字数 3155 浏览 2 评论 0原文

编辑: 我实现了一些逻辑来隐藏我的图标,以及仅在鼠标上的特定行中的特定行时才如何。我需要将此逻辑调整为我的项目。不幸的是,在我的项目中,改变全球风格是不可接受的:( 我试图用主题实现它,但是现在,所有行都张开并显示了所有图标,而不是当前行。是否可以使用这些主题操作员逻辑并仅在库尔特式悬停行上显示图标? 现在,这是我的父组件html:

<ng-container *ngIf="!emptyList else emptyListTemplate">
                            <tr requestRow *ngFor="let request of requests"
                                (mouseenter)="emitEventToChild(true)"
                                (mouseleave)="emitEventToChild(false)"
                                [isRequestRowHighlighted]="eventsSubject.asObservable()"
                                [attr.data-cy-request-id]="request.id"
                                [request]="request"
                                [actionable]="true"
                                [isRequest]="isRequestsList"
                                (delete)="deleteRequest($event)"
                                appMemoTooltip>
                            </tr>
                        </ng-container>

这是TS部分:

isMouseEnter: boolean
eventsSubject: Subject<boolean> = new Subject<boolean>();

emitEventToChild(e: boolean) {
    this.eventsSubject.next(e)
}

private filterSubscription: Subscription

这是我的孩子组件html(其选择器是'tr [requestRow]')

<td *ngIf="actionable" class="p-auto text-center actions" (mouseenter)="memoControls.pause()" (mouseleave)="memoControls.resume()">
<div class="d-flex align-items-center justify-content-end" [ngClass]="{'invisible': !isMouseEnter}">
    <button
        *ngIf="preferredEditType"
        type="button" class="btn btn-primary bg-transparent border-0"
        title="Edit"
        data-cy-id="cy-btn-edit"
        (click)="load($event, request, isRequest, preferredEditType)">
        <i class="icon-edit-request"></i>
    </button>
    <button *ngIf="hateoas.supports(request, 'details')"
            type="button" class="btn btn-success bg-transparent border-0"
            title="Reuse"
            data-cy-id="cy-btn-reuse"
            (click)="load($event, request, isRequest, requestOperationType.REUSE)">
        <i class="icon-copy"></i>
    </button>
    <button *ngIf="hateoas.supports(request, 'delete')"
            type="button" class="btn btn-danger bg-transparent border-0"
            title="Delete"
            data-cy-id="cy-btn-delete"
            (click)="deleteRequest($event)">
        <i class="icon-trash"></i>
    </button>
</div>

这是ts文件:

@Input() isRequestRowHighlighted: Observable<boolean>
isMouseEnter: boolean
ngOnInit(): void {
    this.eventsSubscription = this.isRequestRowHighlighted.subscribe(value => this.isMouseEnter = value)}
enter code here
ngOnDestroy(): void {
    this.eventsSubscription.unsubscribe();
}

预期结果

现在a>

请,让我知道如何仅适用于当前(鼠标)行的图标,而不是所有这些行。 谢谢。

EDIT:
I implemented some logic to hide my icons and how them only when a specific row from parent component is on mouseover. I need to adjust this logic to my project. Changing global styles is unfortunately unacceptable in my project :(
I tried to implement it with Subject, but now instead of current row, all rows are affaected and all icons are shown. Is it possible to use these Subject Operator logic and show icons only on curretnly hovered row?
This is now my parent component HTML:

<ng-container *ngIf="!emptyList else emptyListTemplate">
                            <tr requestRow *ngFor="let request of requests"
                                (mouseenter)="emitEventToChild(true)"
                                (mouseleave)="emitEventToChild(false)"
                                [isRequestRowHighlighted]="eventsSubject.asObservable()"
                                [attr.data-cy-request-id]="request.id"
                                [request]="request"
                                [actionable]="true"
                                [isRequest]="isRequestsList"
                                (delete)="deleteRequest($event)"
                                appMemoTooltip>
                            </tr>
                        </ng-container>

This is the TS part:

isMouseEnter: boolean
eventsSubject: Subject<boolean> = new Subject<boolean>();

emitEventToChild(e: boolean) {
    this.eventsSubject.next(e)
}

private filterSubscription: Subscription

This is my child component HTML (its selector is 'tr[requestRow]' )

<td *ngIf="actionable" class="p-auto text-center actions" (mouseenter)="memoControls.pause()" (mouseleave)="memoControls.resume()">
<div class="d-flex align-items-center justify-content-end" [ngClass]="{'invisible': !isMouseEnter}">
    <button
        *ngIf="preferredEditType"
        type="button" class="btn btn-primary bg-transparent border-0"
        title="Edit"
        data-cy-id="cy-btn-edit"
        (click)="load($event, request, isRequest, preferredEditType)">
        <i class="icon-edit-request"></i>
    </button>
    <button *ngIf="hateoas.supports(request, 'details')"
            type="button" class="btn btn-success bg-transparent border-0"
            title="Reuse"
            data-cy-id="cy-btn-reuse"
            (click)="load($event, request, isRequest, requestOperationType.REUSE)">
        <i class="icon-copy"></i>
    </button>
    <button *ngIf="hateoas.supports(request, 'delete')"
            type="button" class="btn btn-danger bg-transparent border-0"
            title="Delete"
            data-cy-id="cy-btn-delete"
            (click)="deleteRequest($event)">
        <i class="icon-trash"></i>
    </button>
</div>

This is the TS file:

@Input() isRequestRowHighlighted: Observable<boolean>
isMouseEnter: boolean
ngOnInit(): void {
    this.eventsSubscription = this.isRequestRowHighlighted.subscribe(value => this.isMouseEnter = value)}
enter code here
ngOnDestroy(): void {
    this.eventsSubscription.unsubscribe();
}

Expected Result

As is now

Please, let me know how can I achieve displaying icons only for current(on mouseenter) row, not for all of them.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2025-02-18 17:32:35

.css到Mannage两个差异组件应该是全局的,因此请尝试 styles.css

tr[requestRow] button {
  visibility: hidden;
}
tr[requestRow]:hover button{
  visibility: visible;
}

一个简单的

” >更新

好吧,我们可以使用 viewEncapsulation.none .css在我们的组件中以全局方式进行。

@Component({
  selector: [requestRow]
  ...
  encapsulation:ViewEncapsulation.None,
})

但是要小心,如果在我们的组件中我们也使用,例如h1 {color:red}此样式也会以

另一种方式传播到我们所有的aplication(但标记为标记)由于弃用)正在使用某些类似(在parent.css中)

:host ::ng-deep tr[hello]  button {
  visibility:hidden
}
:host ::ng-deep tr[hello]:hover button{
  visibility:visible
}

the .css to mannage two differents components should be global, so try in styles.css

tr[requestRow] button {
  visibility: hidden;
}
tr[requestRow]:hover button{
  visibility: visible;
}

A Simple stackblitz

Update

Well, we can use a ViewEncapsulation.None, who apply all the .css in our component in a way global.

@Component({
  selector: [requestRow]
  ...
  encapsulation:ViewEncapsulation.None,
})

But be carefull, if in our component we use also e.g. h1{color:red} this style is also propagate to all our aplication

Another way (but is marked as deprecated) is using some like (in parent.css)

:host ::ng-deep tr[hello]  button {
  visibility:hidden
}
:host ::ng-deep tr[hello]:hover button{
  visibility:visible
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文