angular - Trouble implementing an Angular2 front-end service -
my goal display logged-in user’s name.
i have back-end express service uses mongoose perform mongodb queries. i’m able see work postman. service queries “users” collection in database. takes userid , returns user’s firstname.
i’m encountering problems implementing angular2 front-end service gets userid
local storage, passes back-end, , retrieves user’s firstname
. want use handlebars string interpolation insert username wherever want.
loggedinuser()
in code below i'm focusing.
auth.service.ts
import { injectable } '@angular/core'; import { http, headers, response } '@angular/http'; import 'rxjs/rx'; import { observable } 'rxjs'; import { user } './user.model'; import { errorservice } '../errors/error.service'; @injectable() export class authservice { constructor(private http: http, private errorservice: errorservice) {} loggedinuser() { const userid = localstorage.getitem('userid'); console.log('service) user id:' + userid); return this.http.get('htp://localhost:3000/user/current/' + userid) .map((response: response) => { const user = response.json().obj; return user; }) };
when try use loggedinuser in component's html file, no output when route rendered. console.log call produces nothing in browser's console; it's if method isn't being called.
signup.component.html
username: {{ loggedinuser }}
https://angular.io/guide/cheatsheet explains various string interpolation techniques..
are calling service in nginit of component. like
export class mycomponent implements ngoninit { user: user constructor (private authservice:authservice) {} oninit{ this.authservice.subscribe((data:user) => this.user = data) } }
html
username: {{ user }}
Comments
Post a Comment