Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call service function from DOM - Angular 2

Is there any way to call the function defined in service from a DOM, I can call components function from DOM but how can we call service function, something like this:

@Injectable()
export class UtilsService {
    constructor() {}

    getSessionId() {}
}

<button name="getsession" (click)="UtilsService.getSessionId()">Get Session</button>
like image 508
Dheeraj Agrawal Avatar asked Sep 30 '16 07:09

Dheeraj Agrawal


1 Answers

The scope for expressions in view bindings is limited to the the components class instance. There is no support to access statics, globals, types (enums), ... directly from the view.

To be able to refer to it in the view, it has to be accessible via the components class instance:

constructor(public utilsService:UtilsService) {}
<button name="getsession" (click)="utilsService.getSessionId()">Get Session</button>
like image 131
Günter Zöchbauer Avatar answered Oct 30 '22 22:10

Günter Zöchbauer