私は最近、新しい 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));
app.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'
}
}
}
ご協力ありがとうございました。