鑑於以下AngularJS:AngularJS服務是通過REST設置屬性無法正確設置
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
雖然這個作品與角度的視圖系統,它認識到'getUser'將恢復很重要* *任意字符串** **或承諾,取決於如果數據緩存或不可─在許多情況下,爲了避免奇怪的競爭條件錯誤(例如緩存承諾並每次返回),使公共API更加一致可能是必要的或謹慎的。 –