ipavlic
ipavlic

Reputation: 4966

How to replace `link` function to target DOM element in Angular 2 component

Link function in an Angular 1 directive makes it possible to target DOM elements, via

link: function (scope, element, attr) {
    // do something with element[0], e.g. put generated graphics
    // inside the node
}

What's a replacement for that in Angular 2?

Upvotes: 3

Views: 1944

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657446

This might do what you want:

@Component({ 
...,
  template: `
  <div #target></div>
`
})
class MyComponent {
  @ViewChild('target') target;

  // lifecycle callback when `this.target` becomes available
  ngAfterViewInit() {
    var graph2d = new vis.Graph2d(
        this.target.nativeElement, dataset, options);
  }
}

Upvotes: 4

Related Questions