G
G
GregIv2017-07-27 16:08:49
Angular
GregIv, 2017-07-27 16:08:49

How to hang events in Angular2 and access attributes for html coming from api?

Good afternoon!
There is a task to pass html with non-standard tags and attributes from Rest API to Angular2.
Once passed, Angular should be able to interact with that html. Process clicks on it, get attribute values, etc.
Tell me which way to dig?
html example:

<some-container>
        <some-tag some-id="123123" >text</some-tag>
</some-container>

It is necessary to process clicks on some-tag, and get the value of some-id

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0X12eb, 2017-07-28
@0X12eb

By means of what do you plan to process these very clicks and get attribute values?
If, for example, you will use jQuery, then this option will do:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser'

@Pipe({ name: 'safeHtml' })
export class SafeHtmlPipe implements PipeTransform {
    constructor(private sanitized: DomSanitizer) { }
    transform(value: string) {
        return this.sanitized.bypassSecurityTrustHtml(value);
    }
}

Then we render the content that came from RestAPI and work with all the tags and attributes that came in:
<div [innerHTML]="data.content | safeHtml"></div>

G
GregIv, 2017-07-29
@GregIv

Yes, that's exactly what I did + created directives for the necessary tags. But html does not turn into angular entities ... How to additionally initialize it, as a template, is not clear (

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question