1

I want to invoke a function from a service that I created when you click on something, but I can't get it to work. I simplified it as much as possible just to see if this type of functionality is even possible.

I have the following HTML:

<a (click)="SellingVarietiesService.goToVarietyDetails(3)">Test</a>

Here is the code for the function I want to invoke:

@Injectable()
export class SellingVarietiesService {    

    constructor(private http: Http) { }

    public goToVarietyDetails(varietyId: any): void {        
        console.log(varietyId);
    }
}

The HTML is being generated via .NET/C# and returned from an HTTP call in a service I created. Is it possible to invoke that function in my service with the HTML that is being returned to me? Do I just have the syntax wrong?

EDIT:

I used DomSanitizer on the HTML that I returned (the HTML that contains the (click) function). Here is the code from my component that used it:

// Get Variety List
getVarietyList() {        
    this.sellingMenuService.getVarieties().subscribe(res => {            
        this.varietyListSelling = this.sanitizer.bypassSecurityTrustHtml(res);
    });
}

And here is the HTML where that gets placed:

<div *ngIf="varietyListSelling" [innerHtml]="varietyListSelling"></div>

Here is an example of the what res might look like:

<div class="varietyName">
    <a class="" (click)="sellingVarietiesService.goToVarietyDetails(1176)" >Starbor</a>
    <a href="#deleteVarietySelling" id="deleteVarietySelling_1176" class="quick-delete fa-minus-button" title="Delete" data-toggle="modal">
        <i class="fa fa-minus"></i>
    </a>
</div>
7
  • How does res looks like? Commented Oct 16, 2016 at 19:37
  • I added to the end of my post. Notice (click)="sellingVarietiesService.goToVarietyDetails(1176) is there, yet doesn't work. Commented Oct 16, 2016 at 19:45
  • It won't work because it should be compiled at first Commented Oct 16, 2016 at 19:47
  • See stackoverflow.com/questions/40063791/… or use dynamic component loading like stackoverflow.com/questions/39410355/… Commented Oct 16, 2016 at 19:49
  • I tried the first one and changed my code to this: this.elRef.nativeElement.querySelector('a.testClick').addEventHandler('click', this.getVarietyDetails.bind(this)); It gave me this error: Cannot read property 'addEventHandler' of null Commented Oct 16, 2016 at 20:42

1 Answer 1

3

You need to inject service into component that hosts your html

@Component({
  template: `<a (click)="svs.goToVarietyDetails(3)">Test</a>`
})
export class MyComponent {
  constructor( public svs: SellingVarietiesService) {}
}

Then you can access it through component property.

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

5 Comments

Yup, I did that, but it still isn't working. I'm wondering if it is because the HTML is served up from my .NET backend. I don't know if that would cause issues but I can't think of any other reason...
If that's the case look into DomSanitizer
I updated my post to show how I used DomSanitizer. Is there a different way I need to be using it?
Property is innerHTML, not innerHtml (:
AFAIK the innerHtml property works like standart property innerHTML

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.