Angular 2 Component - Access DOM (or create Component without template, purely from JS)

后端 未结 5 1277
滥情空心
滥情空心 2021-02-05 06:41

Trying to play with Angular 2 here... understood that it\'s still in alpha.

How do I access DOM elements from Component? Specifically, I would like to use other librarie

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 07:04

    Note: [Deprecated]

    You can use "BrowserDomAdapter"
    https://angular.io/docs/ts/latest/api/platform/browser/BrowserDomAdapter-class.html

    import {BrowserDomAdapter} from 'angular2/platform/browser'
    
    @Component({
      selector: 'dom',
      templateUrl: 'path/to/template.html',
      providers: [BrowserDomAdapter]
    })
    export class DomComponent {
      constructor(private _dom: BrowserDomAdapter) {
        var domIWant = this._dom.query('whatever you want to get');
      }
    }
    

    Process

    1. import BrowserDomAdapter
    2. apply providers: [BrowserDomAdapter]
    3. initiate in constructor()
    4. get dom using instanced BrowserDomAdapter

提交回复
热议问题