0

I'm new to angular. I have added custom attribute to span tag like below

<span data-toggle="modal" data-target="#myModal" style="padding: 2px;cursor: pointer;background: #e60000;color: #fff" (click)="loadnewsdetail()" [attr.newsid]="news.NewsId">Read More </span>

I want to get value of attribute newsid for that I'm doing below code using jQuery

 loadnewsdetail() {
         var a = $(this).attr('newsid');
         alert(a);
}

It giving me undefined. How to do this in angular and why it's undefined?

1 Answer 1

1

instead of getting the newsId you can just pass it as parameter on click

(click)="loadnewsdetail(news.NewsId)"

and get it like this.

 loadnewsdetail(newsId) {

    alert(newsId);
  }

or if you want to get the attribute you can pass an event to the function (actually not needed in your case). but, here is how you can do it.

(click)="loadnewsdetail($event)"

and get that value using

loadnewsdetail(newsId) { 
    alert(newsId.target.getAttribute("newsid"));
}

demo

Sign up to request clarification or add additional context in comments.

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.