gpt4 book ai didi

javascript - 如何在 Angular js中将数据从工厂传递到 Controller ?

转载 作者:数据小太阳 更新时间:2023-10-29 06:06:17 24 4
gpt4 key购买 nike

我有一个工厂包含保存客户功能。成功后我想在 Controller 中传递它的响应以便我可以更新 View 。

工厂

sampleApp.factory("authFactory", function($location, $http, transformRequestAsFormPost) {
return {
saveCustomer: function(data) {
var request = $http({
method: "post",
url: "webservice/ws.php?mode=saveCustomer",
transformRequest: transformRequestAsFormPost,
data: data
});
request.success(
function(response) {
console.log(response);
}
);
}
};
});

Controller

sampleApp.controller('customerController', function($scope, testService,authFactory,$http) {
$scope.addCustomer = function() {
var data = {name: $scope.customerName,city: $scope.customerCity};
// Calling Factory Function
authFactory.saveCustomer(data);
// How to fetch response here
}
});

请帮我解决这个问题谢谢

最佳答案

各种方式,第一个想到的是这样的:

//in your factory
return {
saveCustomer: function(data) {
var request = $http({...});

return request;
}
}

//in your controller
authFactor
.saveCustomer(data)
.success(function() {
//update controller here
})

关于javascript - 如何在 Angular js中将数据从工厂传递到 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28676921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com