0

Few html elements loaded from server side and angular click() event also part of it. but event is not firing after rendering the elements. I understand that i need to notify the DOM. I can not do this.

component.ts

mport { Component} from '@angular/core';
@Component({
    selector: 'app-root',
    templateUrl: '../Views/Dashboard.html',
    providers: []
})

export class DashboardComponent {
    public deviceData="<div (click)="Go()">Click</div>"; //e.x: loaded from server
    constructor(){

    }
    Go():void{
       alert("Go");
    }

}

html

<div [innerHTML]="deviceData"></div>
5
  • Where did you read that it should work? Commented Mar 6, 2018 at 9:58
  • blog.angularindepth.com/… Commented Mar 6, 2018 at 9:59
  • 1
    @AngularInDepth.com could not write such a thing Commented Mar 6, 2018 at 10:00
  • ok, do you have any solution for it, i need help. Commented Mar 6, 2018 at 10:02
  • 1
    stackoverflow.com/… Commented Mar 6, 2018 at 10:05

2 Answers 2

3

You can bind event using

<div class="classname" [innerHTML]="deviceData"></div>

this.deviceData = "<a herf='javascript:void(0);'>Click</a>";

ngOnInit()
{
 var obj=this;
    $(document).on('click', '.classname', function () {
        obj.Go();
    });
}

Go() 
{
    alert("Go");
}
Sign up to request clarification or add additional context in comments.

3 Comments

I appreciate your answer @Manish, but jquery with angular bit overwhelming work. Thanks, let me try with your given code snippets.
thanx ..for using jquery you have to write "declare var $: any;" this line to typing.d.ts
this should be the accepted answer, it solved the problem
0

A click function (or any Angular specific tag) to be rendered as html is not best practice. Try to only render bare basic html and put the click function in separately.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.