Angular2 异常:主机中的 ngClass,"isn't a known native property"
Angular2 Exception: ngClass within a Host, "isn't a known native property"
是否可以在主机内使用 "ngClass" 以获得 component/directive。
@Component({
selector: 'custom',
template: `<div [ngClass]="classMap"></div> // I work
<ng-content></ng-content>`,
host: {
'[ngClass]' : 'classMap' // I don't work!!!
}
})
export class CustomComponent {
constructor () {
this.classMap = {
custom: true
};
}
}
在上面的示例中,ngClass 在模板中的 div 上正常工作。它添加了 "custom" class,但在尝试通过添加时抛出异常给主持人。
"Can't bind to 'ngClass' since it isn't a known native property"
直接在主机中设置 class 可以正常工作,例如;
host: {
'[class.custom]' : 'classMap.custom'
}
所以会觉得ngClass可以吗?语法不正确? (可能!!!):)
ngClass
是指令,主机绑定不支持指令。
host: {
'[ngClass]' : 'classMap' // I don't work!!!
}
需要
host: {
'[class.className]' : 'className',
'[class]' : 'classNames'
}
其中 classNames
是 space 分隔列表 类。
我遇到了同样的问题,但我可以通过在我的模块上导入一些模块来解决它:
import { RouterModule, Routes } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import {KSSwiperModule} from 'angular2-swiper';
import { DashboardComponent } from './dashboard.component';
import {SwiperComponent} from "./swiper.component";
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
];
@NgModule({
imports: [
RouterModule.forChild(routes),
KSSwiperModule,
BrowserModule,
FormsModule,
HttpModule
],
declarations: [ DashboardComponent, SwiperComponent ]
})
export class DashboardModule { }
我的组件不使用 "host":
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
moduleId: "dashboard",
selector: 'app-dashboard',
templateUrl: 'dashboard.component.html',
styleUrls: [ 'dashboard.component.scss' ],
encapsulation: ViewEncapsulation.None
})
export class DashboardComponent {
public isLandingHidden: boolean;
showReasons() {
this.isLandingHidden = true;
}
}
是否可以在主机内使用 "ngClass" 以获得 component/directive。
@Component({
selector: 'custom',
template: `<div [ngClass]="classMap"></div> // I work
<ng-content></ng-content>`,
host: {
'[ngClass]' : 'classMap' // I don't work!!!
}
})
export class CustomComponent {
constructor () {
this.classMap = {
custom: true
};
}
}
在上面的示例中,ngClass 在模板中的 div 上正常工作。它添加了 "custom" class,但在尝试通过添加时抛出异常给主持人。
"Can't bind to 'ngClass' since it isn't a known native property"
直接在主机中设置 class 可以正常工作,例如;
host: {
'[class.custom]' : 'classMap.custom'
}
所以会觉得ngClass可以吗?语法不正确? (可能!!!):)
ngClass
是指令,主机绑定不支持指令。
host: {
'[ngClass]' : 'classMap' // I don't work!!!
}
需要
host: {
'[class.className]' : 'className',
'[class]' : 'classNames'
}
其中 classNames
是 space 分隔列表 类。
我遇到了同样的问题,但我可以通过在我的模块上导入一些模块来解决它:
import { RouterModule, Routes } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import {KSSwiperModule} from 'angular2-swiper';
import { DashboardComponent } from './dashboard.component';
import {SwiperComponent} from "./swiper.component";
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
];
@NgModule({
imports: [
RouterModule.forChild(routes),
KSSwiperModule,
BrowserModule,
FormsModule,
HttpModule
],
declarations: [ DashboardComponent, SwiperComponent ]
})
export class DashboardModule { }
我的组件不使用 "host":
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
moduleId: "dashboard",
selector: 'app-dashboard',
templateUrl: 'dashboard.component.html',
styleUrls: [ 'dashboard.component.scss' ],
encapsulation: ViewEncapsulation.None
})
export class DashboardComponent {
public isLandingHidden: boolean;
showReasons() {
this.isLandingHidden = true;
}
}