为什么我没有从angularJS中的这个工厂函数中得到响应
Why im not getting the response back from this factory function in angularJS
我得到了这个角度工厂:
var productApp = angular.module('productApp', ['ngRoute', 'LocalStorageModule', 'angularSlideables', 'ui.bootstrap']);
productApp.factory('productFactory', function($http, localStorageService, $q) {
var factory = {};
factory.getProductById = function(prod_id) {
if(prod_id !== '') {
$http({
url: 'rest/message/getProductById/' + prod_id,
method: 'GET'
}).success(function(data, status) {
return data;
}).error(function(data, status){
// do nothing
});
}else {
alert("There was an error while passing the ID. Please refresh the page and try again");
}
}
return factory;
});
在控制器中注入工厂并调用"getProductById"函数:
productApp.controller('ModalInstanceCtrl', function ($scope, $modalInstance, productFactory, prodId) {
console.log("this is the prod id " + prodId);
// search product in the database
$scope.prod = productFactory.getProductById(prodId);
console.log($scope.prod);
$scope.ok = function () {
console.log($scope.prodData);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
现在,不知道它出了什么问题…函数RETURNS数据,因为我做了console.log(data)并看到了所有响应,但在控制器中,如果我检查$scope.prod,它是未定义的。它不会从函数返回数据。
(万一你们问,控制器参数中的"prodId"很好,并从另一个控制器中检索)
如何解决此问题?:(
提前谢谢。
我用来解决这个问题的模式是传递成功&错误回调函数到工厂。。。像这样:
var productApp = angular.module('productApp', ['ngRoute', 'LocalStorageModule', 'angularSlideables', 'ui.bootstrap']);
productApp.factory('productFactory', function($http, localStorageService, $q) {
var factory = {};
factory.getProductById = function(prod_id, successCallback, errorCallback) {
if(prod_id !== '') {
$http({
url: 'rest/message/getProductById/' + prod_id,
method: 'GET'
})
.success(successCallback)
.error(errroCallback);
} else {
alert("There was an error while passing the ID. Please refresh the page and try again");
}
}
return factory;
});
然后:
productApp.controller('ModalInstanceCtrl', function ($scope, $modalInstance, productFactory, prodId) {
console.log("this is the prod id " + prodId);
// search product in the database
productFactory.getProductById(prodId, function successCallback(data) {
$scope.prod = data;
}, function errorCallback(data, status) {
alert("An error occurred retrieving product. Please refresh the page & try again.");
});
console.log($scope.prod);
$scope.ok = function () {
console.log($scope.prodData);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
通过这种方式,您可以访问控制器&可以对返回的数据执行任何需要的操作。
以下是我所做的。我使用$resourcence而不是$http,但它应该足以让您继续使用。您甚至可能想要使用$resource,因为它具有内置的fns。
我的工厂:
.factory('WorkOrder', function($resource){
// $resource Usage: $resource(url[, paramDefaults][, actions]);
return $resource('/controller/get/:id.json', {}, {
/*
* By default, the following actions are returned; modify or add as needed
* { 'get': {method:'GET'},
* 'save': {method:'POST'},
* 'query': {method:'GET', isArray:true},
* 'delete': {method:'DELETE'} };
*/
});
})
我的控制器:
// get the work order data using the work order id from the tag attribute
var getWO = function() {
WorkOrder.get({woId:$attrs.id},
// success callback
function(response) {
// Assign the work order data to the scope
$scope.WorkOrder = response.WorkOrder;
},
// fail callback
function(response) {
// whatever...
}
);
};
getWO();
我把成功和失败的fns放在我的控制器中,因为这是我最可能知道如何应对成功或失败呼叫的地方。我还将函数分配给一个变量,然后立即调用它,以防我想在$timeout中包含fn调用或在其他地方调用它。
希望这能回答你的问题。
相关文章:
- Jasmine测试工厂函数调用本地函数和另一个函数-获取错误:应该是间谍,但得到了function
- 为什么我没有从angularJS中的这个工厂函数中得到响应
- 工厂函数给出“;错误:未定义
- 什么是jQuery中的工厂函数
- AngularJS 工厂函数无法访问本地存储的缓存变量
- 工厂函数不返回数据
- 打字稿中的工厂函数语法
- 调用工厂函数会触发错误
- 在JS模块加载的上下文中,什么是立即执行的工厂函数
- Angular工厂函数需要返回一些东西吗
- 如何从外部调用Angularjs工厂函数
- AngularJS文档:工厂函数没有返回任何内容
- 构造函数与工厂函数
- 在Angular中,如何等待工厂函数完成并处理输出?
- Angular js的模态弹出式调用工厂函数
- 在AngularJS中,如何让一个工厂函数引用另一个工厂函数?
- AngularJS控制器使用工厂函数
- 如何定义一个同名的Typescript构造函数和工厂函数
- ES.下一个装饰器可以直接调用,也可以作为工厂函数调用
- 工厂函数的依赖注入(AngularJS)