angularjs - service not returning value -


I am working to write a service to return the username.

  var username, $ true promise; Function ('user service', function ($ http) {$ promise = $ http.get ('/ api / getuser'). Cvt (function (data) {user name = data;}); $ promise. Then (function () {User name back;}}}}   

But injecting this service into a controller will return an undefined value

  angular .module ('TestApp'). Controller ('UserLoginController', Function ($ Scope, UserService) {console.log ("User Name:" + UserService);});   

I have confirmed that the request received from a legitimate user I return the name. I am quite new to angular and really appreciate it if someone tells me what I am doing wrong here.

This code looks like spaghetti up here is a basic factory you should do:

  app.factory ('UserService', ['$ Http ', function ($ http) {var userService = {}; userService GetUser = function () {return $ http.get (' / api / getuser '). Then (function (res) {return res.data;}, Function (error) {console.log (error); return []; });} Return User Service;}]);   

Then call it in an administrator:

  app.controller ('MyController', ['$ scope', 'UserService', function ($ Scope), UserService) {$ scope.user = {}; UserService.getUser () Then (function (data) {$ scope.user = data.users [0];});}]);  {users: [ID: "34534534", name: "John"}]}  comes back from your API.  

Please note that I wrote it on the fly and did not try to take it out. It should work though.

WARNING: I've just edited my code to fix some mistakes.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -