【问题标题】:Angular2 Detect if element in template view has classAngular2检测模板视图中的元素是否具有类
【发布时间】:2016-10-27 09:32:32
【问题描述】:

我们使用引导程序,有时它会自动将类添加到 DOM 元素。附加到这些元素并检测何时将particalr css 类添加到组件模板子元素的最佳方法是什么?

假设我有这个组件:

import { Component, ViewChild, ElementRef } from '@angular/core';
import { HeaderService } from './header.service';

@Component({
    selector: 'header-comp',
    templateUrl: './Home/RenderLayoutHeader'
})

export class HeaderLayoutComponent {
    constructor(private _headerService: HeaderService) { }
}

这是我的视图模板的一部分:

<header-comp>      
<li class="nav-header-icon-list-item">
                        <div class="overlay-dropdown dropdown" id="patientDDL">
                            <button  class="btn btn-default dropdown-toggle session-menu-container" type="button" id="session-dropdown-menu" data-toggle="dropdown" data-backdrop="true" data-dismiss="modal"  aria-haspopup="true" aria-expanded="false">
                                <img data-initials="ER" src="https://lorempixel.com/40/40/abstract/" class="img-circle session-user-profile-img">

当 bootstrap 将“open”类添加到#patientDDL 元素并在我的组件中执行函数时,我如何在我的组件中进行检测?

谢谢!

编辑: 我根据 Gunter 的解决方案修改了我的组件,但是当我没有在标准之前进行空检查时,我得到了一个空引用)

import { Component, ViewChild, ElementRef, DoCheck } from '@angular/core';
import { HeaderService } from './header.service';

@Component({
    selector: 'header-comp',
    templateUrl: './Home/RenderLayoutHeader'
})

export class HeaderLayoutComponent implements DoCheck {

    @ViewChild('patientDDL') patientDropDownList: ElementRef;

    constructor(private _headerService: HeaderService) { }

    ngDoCheck() {
        console.log('ngDoCheck called');
        if (this.patientDropDownList && this.patientDropDownList.nativeElement.classList.contains('open')) {
            this._headerService.setPatientDDLOpen(true);
        } else {
            this._headerService.setPatientDDLOpen(false);
        }

    }
}

此外,控制台语句在模板加载时记录了 4 次,但随后再也不会调用它,即使在多次添加/删除类之后也是如此。

这是 angular2 rc1 不确定是否相关。

【问题讨论】:

    标签: angular


    【解决方案1】:

    添加模板变量以便能够查询元素。

    <div #patientDDL class="overlay-dropdown dropdown" id="patientDDL">
    

    查询元素

    @ViewChild('patientDDL') patientDDL:ElementRef;
    

    实现ngDoCheck(),在变更检测运行时检查是否添加了类:

    ngDoCheck() {
      if(patientDDL.nativeElement.classList.contains('open')) {
        this.doSomething();
      }
    } 
    

    或某些特定事件

    @HostListener('click', ['$event'])
    clickHandler(event) {
      if(patientDDL.nativeElement.classList.contains('open')) {
        this.doSomething();
      }
    } 
    

    【讨论】:

    • 我只是在想你不能用@HostBinding 做任何事吗?
    • 您可以设置和删除一个类,但 Angular2 不提供从 DOM 元素中读取的任何内容。
    • ngDoCheck 什么时候触发?我问是因为单击元素并添加类时它似乎没有触发。
    • @GünterZöchbauer 是foobar.nativeElement.classList.contains('open') 一个好的做法,还是有其他方法可以做到这一点(使用renderer 或其他任何方法)?谢谢:)
    • @sebaferreras renderer(或一般的 Angular)没有提供任何关于从 DOM 读取的信息。如果需要从 DOM 中读取,直接 DOM 访问是唯一的方法。不过,在纯 Angular 应用程序中阅读 classList 是不必要的。有些事情是无法避免直接 DOM 访问的,即使在纯 Angular 应用程序中也是如此。
    猜你喜欢
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2019-11-17
    • 2019-02-12
    • 2021-05-24
    相关资源
    最近更新 更多