嗨我是角度js的新手,我想把空的$ scope传递给我的$ factory用于动态登录我使用$ http获取我的API中的数据我试图将范围放入工厂但我没有& #39; t work
这是我的serviceAPI
(function() {
"use strict";
angular.module('starter').factory(serviceAPI', function($http, $q, $ionicLoading, $timeout) {
function getData() {
var deferred = $q.defer();
$ionicLoading.show({ template: 'Loading...' });
var url = "myAPI";
var data = {
username: "admin", <-- this is the one I want to dynamic it
password: "admin" <--
};
$http({
method: 'POST',
url: url,
data: data
}).success(function(data) {
$ionicLoading.hide();
deferred.resolve(data);
}).error(function() {
console.log('Error while making HTTP call');
$ionicLoading.hide();
deferred.reject();
});
return deferred.promise;
}
//a Return value to public
return {
getData: getData,
};
})
}());
这是控制器
(function() {
"use strict";
angular.module('starter').controller('LoginCtrl', ['$scope', 'serviceAPI', LoginCtrl]);
function LoginCtrl($scope, serviceAPI) {
$scope.username = "";
$scope.password = "";
$scope.login = function() {
serviceAPI.getData().then(function(data) {
console.log(data)
});
}
}
}());
答案 0 :(得分:2)
at service api
function getData(userNameIn, passwordIn) {
//...
var data = {
username: userNameIn,
password: passwordIn
}; //...}
在控制器处 ...
$scope.username = "";
$scope.password = "";
$scope.login = function() {
serviceAPI.getData($scope.username,$scope.password).then(function(data) {
console.log(data)
});
}
...