I am using AngularJs with Coffee-Script, and I have a requirement to pass the current login username to the backend service in header which means I have to retrieve the username through another request. However the $resource service to perform REST request in AngularJs seems to be async, so I can't get the username though the following code:
'use strict'
angular.module('testapp.service').factory 'TestController', [
'$resource', 'userService'
($resource, userService) ->
userService.getUser().then (user) ->
console.log(user.name) # <- line 8
username = user.name # <- line 9
$resource "/api/path", {},
get:
method: 'GET'
headers:
'username': username
]
With line 8, I could get the username printed to the console. With line 9, I can't pass it to the headers field below.
Every time, when the page refreshed, the console get printed the username, but when I click on the button, there is no printing again. Please help, any kind of suggestion would be helpful and appreciated !
I am using AngularJs 1.4