2016-01-13 8 views
5

Google Maps InfoWindowをビルドするときに、Angular2テンプレート構文を使用したいと考えています。Angular2コンポーネントを文字列にレンダリングするにはどうすればよいですか?

私が知る限り、これは、テンプレート文字列としてコンポーネントをInfoWindowコンストラクタオブジェクトのcontentプロパティに渡すことを意味します。

これが当てはまる場合は、コンポーネントテンプレートを文字列化する必要があると思います。これは正しいのですか?ここで

は私が行う必要がある思い ものの例である:

// Component 

import {Component} from 'angular2/core'; 
import {Thing} from './something/in/my/app/thing.model.ts'; 

@Component({ 
    selector: 'my-infowindow', 
    template: `<p>This is an infowindow for {{ something.name }}</p>` 
}) 
export class MyInfoWindowComponent { 
    constructor(public something: Thing) {} 
} 


// Implementation 

import {Thing} from './something/in/my/app/thing.model.ts'; 
import {MyInfoWindowComponent} from './path/to/infowindow.component'; 

/* { ...stuff... } */ 

private _buildInfoWindow(thing: Thing): google.maps.InfoWindow { 
    return new google.maps.InfoWindow({ 
     /* v this is what I want v */ 
     content: new MyInfoWindowComponent(thing).toString() 
     /*^this is what I want^*/ 
    }); 
} 
+0

'ElementRef'を見てみましょう:HTTPS ://angular.io/docs/ts/latest/api/core/ElementRef-class.htmlまたは「ViewChild」:https://angular.io/docs/ts/latest/api/core/ViewChild-var.html – Langley

+0

Angularコンポーネントを作成して文字列化してinfowindoに渡すことができませんw。代わりに文字列を渡すのはなぜですか?データバインディングを使用したいからですか?とにかくこれを情報ウィンドウに渡した後は更新されません。 –

+1

@GünterZöchbauer - そうですね、私はデータバインディングを必要としません。テンプレートのsytaxはひどいstringbuilderよりもはるかに良いようです。 (特に 'ngIf'と' ngFor'の場合)。それは理にかなっていますか? – drewwyatt

答えて

2

ElementRef angular.io/docs/ts/latest/api/core/ElementRef-class.htmlを渡して試してみてください。

ViewChild

か何か:

https://angular.io/docs/ts/latest/api/core/ViewChild-var.html

私はあなたのためのコードを書くことはありませんが、アイデアはこのようなものです:

@Component({ 
    selector: 'my-infowindow', 
    template: `<p #kiddo>This is an infowindow for {{ something.name }}</p>` 
}) 
export class MyInfoWindowComponent { 
    @ViewChild('kiddo') viewChild; 
    constructor(public something: Thing) {} 
    ngOnInit(){ 
     yourFunction(this.viewChild); //or viewChild.innerHtml or w/e works 
    } 
} 
+0

申し訳ありませんが、明確ではありません。コンポーネントのviewChildプロパティからコンパイルされたHTMLをgoogle.maps.InfoWindow content:stringに挿入するにはどうすればよいですか? –

関連する問題