2017-09-16 8 views
1

私はgoogleマップと同じような角度の何かをしようとしています。だから私は背景画像/マップを持っていて、私はマウスのクリックした地図の代わりにオブジェクト/コンポーネントを作成したいと思います。クリックしたイベントの代わりにコンポーネントを作成する

今はx/y値を取得しています。しかし、私はクリックイベントの後に何をすべきか分かりません。

getCoordinates(event): Coordinates { 
    let offsetLeft = document.getElementById("drawing").offsetWidth; 
    let offsetTop = document.getElementById("drawing").offsetHeight; 
    let x = event.pageX - (document.getElementById("drawing").offsetLeft); 
    let y = event.pageY - (document.getElementById("drawing").offsetTop); 
    console.log("zoom " + this.zoom); 
    console.log("x " + x/this.zoom); 
    console.log("y " + y/this.zoom); 
    console.log("element " + offsetLeft); 
    console.log("element " + offsetTop); 
    return new Coordinates(x,y); 
    } 

理想的には、ユーザーがフォームをクリックして埋め込む場所に小さなポップアップを生成したいと考えています。

答えて

2

作成したコンポーネント

<my-component *ngFor="let cmp of components" [info]="cmp"></my-component> 

export class ParentComponent { 
    components:Coordinates[] = []; 

    getCoordinates(event): Coordinates { 
    let offsetLeft = document.getElementById("drawing").offsetWidth; 
    let offsetTop = document.getElementById("drawing").offsetHeight; 
    let x = event.pageX - (document.getElementById("drawing").offsetLeft); 
    let y = event.pageY - (document.getElementById("drawing").offsetTop); 

    this.components.push(new Coordinates(x,y)); 
    } 
} 

あなたは部品の異なる種類を追加したい場合は、あなたが可能なコンポーネントの位置自体

export class MyComponent { 
    @Input() info:Coordinates; 

    @HostBinding('style.left.px') 
    get left() { return this.info.left; } 

    @HostBinding('style.top.px') 
    get top() { return this.info.top; } 
} 

Plunker Example

を作るには、座標情報を渡しますアイデアを使用して上記のアプローチを拡張してからAngular 2 dynamic tabs with user-click chosen components

+0

Angular2でgetElementByIdのようなJavascriptを使用するのは良い方法ですか? – RGS

+1

正確な内容がわかりません。一般的に、または何らかの形でこの質問に関連していますか? TypescriptとAngularバインディングはJSに変換されるので、JSを避ける​​ことはできません。 TypeScriptではなくJSを意味しますか、またはjQueryのようなJSライブラリを使用していますか? –

+0

はい。つまり、Angular2でJavaScriptを使用する代わりに、ElementRef、ViewChildなどのAngular2でTypeScriptを使用できます。私の同僚たちは、Angular2でのJavaScriptの使い方はquerySelectorAllのようにしないようにしています。あなたはあなたの答えにdocument.getElementByIdを使用しました。それは私が疑問に尋ねた理由です。 – RGS

関連する問題