Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I enable click on a svg line in angular2

I have a line drawn through svg and I need to catch the click event on it. However when I set the onclick to a function, I get an error.

 <div id ="middle" class="col-lg-2 col-xs-3">  
      <svg width="300" height="2000" xmlns="http://www.w3.org/2000/svg">  
             <line id="line2"   [attr.x1]= "from_x0"   
              [attr.y1]="from_y0"  [attr.x2]="to_x"  
              [attr.y2]="to_y" stroke-width="2" stroke="green" 
              onclick ="OnClick()"//>
     </svg>
</div>

The error I am getting is

Uncaught ReferenceError: OnClick is not defined at SVGLineElement.onclick (:3000/#/app/tables/tablelist/tableedit/1:1)

My OnClick function is below

OnClick (){
  console.log('clicked on line')
}

Would appreciate any pointers to resolve this.

like image 974
sv16 Avatar asked Oct 18 '22 18:10

sv16


1 Answers

Angular event binding is (event)="..."

<line id="line2"   [attr.x1]= "from_x0" [attr.y1]="from_y0"   
 [attr.x2]="to_x" [attr.y2]="to_y" stroke-width="2" stroke="green"   
 (click)="OnClick()"/>
like image 143
Günter Zöchbauer Avatar answered Oct 31 '22 16:10

Günter Zöchbauer