Eduard Arevshatyan Eduard Arevshatyan - 3 months ago 20
TypeScript Question

POST (http service) angular 2

I have this POST data in my angular 2 application:

postServices(property) {
let body = JSON.stringify(property);
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({ headers:headers });
return this._http.post(this._url, body, options)
.map(res => res.json());
}


And in my template i have this button:

<button *ngIf="!!service_rec.servicecontrolled"
[style.background-color]="service_rec.controlled == 'true' ? 'green' :'orange'"
class="btn btn-warning">
{{ service_rec.servicecontrolled | json | toOnOff }}
</button>


And how i can add event on
(click)=""
in my template with this http service?

Answer

you can add a click handler on button through which you can call postService.

<button *ngIf="!!service_rec.servicecontrolled" (click)= "callingPostService()"
 [style.background-color]="service_rec.controlled == 'true' ? 'green' :'orange'"
         class="btn btn-warning">
         {{ service_rec.servicecontrolled | json | toOnOff }}
 </button>

in your component, something like this would do

callingPostService() {
    this._myService.postService('prop');
}
Comments