V. Aliosha V. Aliosha - 3 years ago 82
AngularJS Question

Convert AngularJS service to Angular

How to convert this AngularJS to Angular?
I created component named

ForgotPassword
and have no idea how to correctly send
http.post
to my c# controller.

function (window, angular) {
"use strict";

angular.module("Mobile")
.service("accountService", ["$http", function ($http) {
return {

forgotPassword: function (email, success, error) {
$http.post("Account/ForgotPassword", { Email: email}).success(success).error(error);
}
}
}]);
})(window, window.angular);

Answer Source

You would need to create a service class

app.serivce.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';

@Injectable()
export class AppProvider {    
  constructor(private http: Http) {
  }

  public forgotPassword(email: string):Observable<any> {
    let url:string = 'apiUrl/Account/ForgotPassword';
    let body: any = { Email: email }

    return this.http.post(url, body).map((res:Response) => res.json());
  }
}

This method would be called from inside your component.ts file

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download