I am requesting data from API through service.ts. i want server response into my component's local variable for further manipulation.i have tried with subscribe method, map method, Observable method..but all are giving sort of error..whose solution is not available on Internet.
here is hierarchy...
\auth
-- html,ts,css,spec.ts
-- auth-service.ts
here is authcomponent.ts
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { AuthService } from './auth.service';
import { Router } from '@angular/router';
import {LocalStorage, SessionStorage} from "angular-localstorage/";
import { error } from 'selenium-webdriver';
@Component({
selector: 'app-auth',
templateUrl: './auth.component.html',
styleUrls: ['./auth.component.css'],
providers:[AuthService]
})
export class AuthComponent implements OnInit {
registerForm: FormGroup;
loginForm : FormGroup;
isLogin = true;
result : any;
constructor(private authService:AuthService, private router: Router ) {
}
this.loginForm = new FormGroup ({
username: new FormControl(),
password: new FormControl()
})
onLogin(){
this.authService.login(this.loginForm.value)
.map(
res =>
{
this.result = res;
},error => {
console.log(error);
}
)
}
}
auth-service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';;
import { LocalStorage, SessionStorage } from "angular-localstorage/";
import { Configuration } from '../config'
import 'rxjs/add/operator/map';
import { Router } from '@angular/router';
declare var $: any;
@Injectable()
export class AuthService {
constructor(private http:HttpClient, private config: Configuration, private router:Router) { }
api_url = this.config.ServerWithApiUrl;
login(newLogin){
return this.http.post( this.api_url+'login', newLogin);
}
}