//controller pour connection to API
.controller('LoginConnect', ['$scope', 'connecting',
function($scope,connecting){
$scope.user = {};
var users = $scope.user;
$scope.connect = function (users) {
var log = $scope.user.login;
var pass = $scope.user.password;
var mydata = {};
connecting.login(log,pass,mydata);
$scope.datab = mydata;
};
}
])
//factory pour aller chercher le token
.factory('connecting', ['$http','$q', function ($http,$q){
var token;
var ConnectingFactory = {};
ConnectingFactory.login = function(log,pass){
var deferred = $q.defer();
$http({
method: 'POST',
url: "http://api.tiime-ae.fr/0.1/request/login.php",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: {login: log, password: pass}
})
.success(function(result){
deferred.resolve(result);
var promise = deferred.promise;
promise.then(function(result){
var mydata = result["data"];
}
);
})
};
return ConnectingFactory;
}]);
;
嗨,我是AngularJS中的新成員,我想知道如何將從API接收的數據發送到全局變量並將其注入$ scope。在這個例子中,我想從$ q發送我的結果並在我的視圖中顯示它。
關於如何有效地使用「$ q」的很好的解釋:http://www.codelord.net/2015/09/24/$q-dot-defer-youre-doing-it-wrong/ – Aliz