2017-11-23 16 views
1

動的に作成されたコンポーネントをAngular4でスタイルできますか?だから私は、コンポーネントを作成すると、その後は比較的マウスを左位置し、トップを設定スタイルが動的に作成された角度コンポーネント

createComponent(event) { 
    let componentFactory = this.componentFactoryResolver.resolveComponentFactory(SbImageGalleryPinComponent); 

    let componentRef = this.pin.createComponent(componentFactory); 

    document.querySelector('.sb-component-gallery__container').addEventListener('mousemove', this.onMouseMove.bind(this)); 
} 

onMouseMove(event) { 
    // ???? 
} 

: は、私は次のコードを持っています。私はマウスに粘着性のあるコンポーネントを作成しています。私が必要とするロジックを再現することは可能ですか?あなたが動的に追加されたコンポーネントに、以下に示すように結合ホストを追加し、コンポーネントが作成された後にバウンドプロパティに値を割り当てることができます

答えて

1

@HostBinding('style.left.px') 
left:number; 

@HostBinding('style.top.px') 
top:number; 
componentRef.instance.left = 50; 
componentRef.instance.top = 30; 

それはする必要があるかもしれませんその後

componentRef.changeDetectorRef.detectChanges(); 

としてください。

はあなたよりもhttps://angular.io/api/core/ComponentRef

+0

参照してください! これは役に立ちます。 – SashaSemanyuk

+0

これはあなたのために聞いてうれしい:) –

関連する問題