角度2のコンポーネントを作成して、HTMLから値を渡すことができます。私はElementRef
を使用すると思ったが、エラーなしで参照することはできない。Angular2コンポーネントElementRefが動作しない
は、ここに私のコードです:
import { Component, ElementRef } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<p>Hello World</p>
`
})
export class MyAppComponent {
constructor(private el: ElementRef) {}
ngOnInit() {
this.el.nativeElement.style.backgroundColor = 'red';
}
}
このコードは、ちょうど私が制御するか、または少なくとも部品のDOM要素をチェックすることができるかどうかのテストのためですが、それは動作しません。
後、私は私が取得していますエラーは、私が最近リリースされた2.0.0バージョン(ないRCを使用しています
Unhandled Promise rejection: Can't resolve all parameters for MyAppComponent: (?). ; Zone: <root> ; Task: Promise.then ; Value: Error: Can't resolve all parameters for MyAppComponent: (?).
ある
<my-app variable="value"></my-app>
のようなHTMLから変数を取得することができるようにしたいです)
エラーが表示されますか?私はelementRefがサポートされていない場合があることを読みました。 – Clint
未処理プロミス拒否:MyAppComponentのすべてのパラメータを解決できません:(?)。 ;ゾーン:;タスク:Promise.then;値:エラー:MyAppComponentのすべてのパラメータを解決できません:(?)。 –
user2791747
この質問をチェックして、私はそれがあなたの問題を解決すると思うhttp://stackoverflow.com/questions/34970778/get-root-component-elementref-or-componentref-angular-2 – Clint