我创建了以下简单的示例组件,它使用@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)