私はAngular2とTypeScriptを初めて使用し、ベストプラクティスに従おうとしています。
単純なJavaScriptモデル({})を使用する代わりに、TypeScriptクラスを作成しようとしています。
ただし、Angular2はそれを好まないようです。
私のコードは:
import { Component, Input } from "@angular/core";
@Component({
selector: "testWidget",
template: "<div>This is a test and {{model.param1}} is my param.</div>"
})
export class testWidget {
constructor(private model: Model) {}
}
class Model {
param1: string;
}
そして私はそれを次のように使用しています:
import { testWidget} from "lib/testWidget";
@Component({
selector: "myComponent",
template: "<testWidget></testWidget>",
directives: [testWidget]
})
Angularからエラーが発生します:
例外:testWidgetのすべてのパラメーターを解決することはできません:(?)。
だから、モデルはまだ定義されていないと思いました...一番上に移動します!
今を除いて、私は例外を受け取ります:
元の例外:モデルのプロバイダーはありません!
どうすればこれを達成できますか?
編集:答えてくれてありがとう。それは私を正しい道へと導きました。
これをコンストラクターに挿入するには、コンポーネントのプロバイダーに追加する必要があります。
これは機能しているようです。
import { Component, Input } from "@angular/core";
class Model {
param1: string;
}
@Component({
selector: "testWidget",
template: "<div>This is a test and {{model.param1}} is my param.</div>",
providers: [Model]
})
export class testWidget {
constructor(private model: Model) {}
}