相关疑难解决方法(0)

组件装饰器的host属性中的ngClass不起作用

我创建了以下简单的示例组件,它使用@Component装饰器的host属性向组件DOM元素添加一些属性和侦听器.在我的情况下[ngClass]没有效果.有人知道为什么以及如何解决它?

import {Injector, Component} from "angular2/core";
import {NgClass} from "angular2/common";
import {SelectionService} from "../selection-service"

@Component({
  selector: 'my-component',
  template: `<div>inner template</div>`,
  host: {
    'style': 'background-color: yellow', // works
    '[ngClass]': "{'selected': isSelected}", // does not work
    '(mouseover)': 'mouseOver($event)', // works
    '(mouseleave)': 'mouseLeave($event)' // works
  },
  directives: [NgClass]
})
export class MyComponent {
  private isSelected = false;

  constructor(private selectionService:SelectionService) {
    this.selectionService.select$.subscribe((sel:Selection) => {
      this.isSelected = sel; // has no effect on ngClass
    });
  }

  mouseOver($event) {
    console.log('mouseOver works');
  }

  mouseLeave($event) {
    console.log('mouseLeave works'); …
Run Code Online (Sandbox Code Playgroud)

angular2-template angular

12
推荐指数
2
解决办法
4193
查看次数

标签 统计

angular ×1

angular2-template ×1