简体   繁体   English

Angular2 - 指令选择器上的ngclass

[英]Angular2 - ngclass on directive selector

I'm using the latest Angular 2 beta (45?) and am trying to attach an ngClass to the element identified by the directive selector. 我正在使用最新的Angular 2 beta(45?)并尝试将ngClass附加到指令选择器标识的元素。 The variable used to toggle the class does not update. 用于切换类的变量不会更新。

I suspect it's one of two things: 我怀疑这是两件事之一:

  1. Angular2 just doesn't work that way, so it's a scoping issue; Angular2只是不起作用,所以这是一个范围问题; if I put a div inside of the directive selector element and attach the ngClass to that div, it works. 如果我在指令选择器元素中放置一个div并将ngClass附加到该div,它就可以工作。 I'd prefer not to do that though. 我不想这样做。
  2. Because i'm reacting to a window event. 因为我对一个窗口事件作出反应。 I've tried using Observers, setTimeout hackery, and zones. 我尝试过使用Observers,setTimeout hackery和zone。 No luck. 没运气。

Is there a way to do what I'm trying to do? 有没有办法做我想做的事情? Is that just not how it's done anymore? 难道这不再是它的完成方式了吗?

Sample code written from memory since I do not have access to it presently: 从内存写入的示例代码,因为我目前无法访问它:

@Directive({
    selector: "nav",
    templateUrl: "templates/nav.html"
});

export class NavComponent {
    isOpen:Boolean = false;

    onScroll(e) {
        this.isOpen = true;
    }

    window.onscroll = (e) => this.onScroll.call(this);  // .call seemed necessary, because otherwise the scope of 'this' changed to window instead of the directive  
}

The following does not work: 以下不起作用:

index.html 的index.html

<nav [ngClass]="{open: isOpen}"></nav>

templates/nav.html 模板/ nav.html

<a href="#">whatever</a>
...
<a href="#">whatever 2</a>

The following does work: 以下工作:

index.html 的index.html

<nav></nav>

templates/nav.html 模板/ nav.html

<div [ngClass]="{open: isOpen}">
    <a href="#">whatever</a>
    ...
    <a href="#">whatever 2</a>
</div>

In addition to Günter's answer, use a host listener instead of assigning an event handler to window.onscroll . 除了Günter的答案,使用主机监听器而不是为window.onscroll分配事件处理程序。 Then Angular change detection will run and actually update the CSS class when you scroll. 然后运行角度变化检测并在滚动时实际更新CSS类。 (Otherwise, some other event will need to happen before the view gets updated.) (否则,在视图更新之前需要执行其他一些事件。)

@Component({
    selector: "nav",
    template: `<a href="#">whatever</a>
      <a href="#">whatever 2</a>`,
    host: {
      '(window:scroll)': 'onScroll($event)',   //  <<------
      '[class.open]': 'isOpen'
    }
})
export class NavComponent {
  isOpen:Boolean = false;
  onScroll(e) {
    this.isOpen = true;
  }
}

@Component({
  selector: 'my-app',
  template: `<nav></nav>
    <div *ngFor="#row of dummyArray; #i = index">row {{i}}</div>`,
  styles: ['.open { background-color: lightblue }']
  directives: [NavComponent]
})
export class AppComponent {
  dummyArray = new Array(100);
  constructor() { console.clear(); }
}

Plunker Plunker

isOpen in isOpen

<nav [ngClass]="{open: isOpen}"></nav>

refers to the parent component of <nav> , the component that has <nav> in its template. 指的是<nav>的父组件,该组件在其模板中具有<nav>

If you want to set the class on the directives host, use 如果要在指令主机上设置类,请使用

@Directive({
    selector: "nav",
    templateUrl: "templates/nav.html",
    host: {'[class.open]': 'isOpen'}
});
export class NavComponent {
    isOpen:Boolean = false;

    onScroll(e) {
        this.isOpen = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM