给定以下AngularJS:
var app = angular.module('testing',[]);
app.service('auth',function($http)
{
var user = '';
$http.get("http://localhost/auth/user").success(function(data)
{
user = data.user;
console.log("User: " + user); //prints Woot4Moo
});
return{
getUser: function(){
return user;
}
};
});
JSON响应:
{"user":"Woot4Moo"}
控制器中的用法:
app.controller('controller',function($http,auth)
{
$http({
url:"http://localhost/my/rest/call/",
method: "GET",
params: {person: auth.getUser()} //this comes back empty
}).success(function(data){...}
};
所以重申我如何让我的最终网址是这样的:
http://localhost/my/rest/call/?user='Woot4Moo'
而不是像这样无效:
http://localhost/my/rest/call/?user= <- the empty string