我最近对新的 Angular2 框架做了很多工作。在测试一些功能时,我最终遇到了错误:
Can't bind to 'ngStyle' since it isn't a known native property
在调查错误本身时,我找到了几种解决方案,例如将“指令:[NgStyle]”添加到组件中,但这并不能解决问题。
代码如下:
main.ts
import {bootstrap} from 'angular2/platform/browser';
import {App} from './app'
bootstrap(App).then(error => console.log(error));
应用程序.ts
import { Component } from 'angular2/core';
import { Button } from './button';
import { NgStyle } from "angular2/common";
@Component({
selector: 'app',
template: '<h1>My First Angular 2 App</h1><button>Hello World</button>',
directives: [Button, NgStyle]
})
export class App { }
按钮.ts
import {Component} from "angular2/core";
import {NgStyle} from "angular2/common";
@Component({
selector: 'button',
host: {
'[ngStyle]': 'style()'
},
templateUrl: '<ng-content></ng-content>',
directives: [NgStyle]
})
export class Button {
style() {
return {
'background': 'red'
}
}
}
谢谢您的帮助。