0

I have a file svg imported, with Jquery and I would Like add directives:

$(seat).attr("myHighlight","");

this "myHighlight" is my directive was added dinamically using jquery my directive is:

import { Directive, ElementRef, Input } from '@angular/core';
declare var $:any;
@Directive({ 
  selector: '[myHighlight]' 
})
export class HighlightDirective {
  constructor(el: ElementRef) {
    $(el.nativeElement).css("fill","red");
  }
}

now my component where I will use to this directive is

import { Component, OnInit, ViewChild,ElementRef, AfterViewInit } from 
'@angular/core';
import {Hero} from './hero';
import {HeroService} from './hero.service';

declare var $:any;

@Component({
  selector: 'my-heroes',
  template:
    <h2 myHighlight>My Heroes</h2>
    <div #contentForSvg>
    </div>
  providers:[HeroService],
})
export class HeroesComponent implements OnInit,AfterViewInit {
  @ViewChild('contentForSvg') contentForSvg : ElementRef;

  constructor(private heroservice:HeroService){}

  ngOnInit(): void {

  this.getHeroes();
    $('<div></div>').addClass('plano-map-image').load('/aecid.svg', 
    (index:any,svg:any)=> {
      $("svg > g").each((row_index:any, g:any)=> {
        $(g).attr('row-id',row_index);
        $(g,"> g").children().each((seat_index:any,seat:any)=> {
          //seat
          $(seat).attr("myHighlight","");
        });
      });
    }).appendTo(this.contentForSvg.nativeElement);
  }

  title='Tour of Heros'
  name = 'Windstorm';
  heroes:Hero[];
  selectedHero: Hero;

  getHeroes(): void{
    this.heroservice.getHeroesSlowly().then(
      heroes=> this.heroes=heroes
    );
  }

  onSelect(hero:Hero):void {
    this.selectedHero= hero;
  }

  onCulturalMap():void{
  }
}

I add attribute myHighlight to tag svg but it not is a directive because not exist in the template of Angular, it will be added after in the event onInit How would I do this, if I want it to be a directive

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Darlyn
  • 310
  • 1
  • 2
  • 16
  • Angular doesn't process bindings, component- or directive- selectors for HTML added dynamically. This only works if it's added statically to a components template. Then only exception is components created at runtime like shown in http://stackoverflow.com/questions/34784778/equivalent-of-compile-in-angular-2/37044960#37044960 – Günter Zöchbauer Apr 07 '17 at 20:48
  • ok,thanks, now it too for directives ? , because I see and it is used for component, thanks. – Darlyn Apr 08 '17 at 00:28

0 Answers0