我得到这个角度厂:为什么IM没有得到响应返回从此工厂功能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');
};
});
现在,不知道它有什么问题...函数返回的数据,因为我做了一个console.log(数据),并看到所有的响应,但在控制器,如果我检查$ scope.prod ,它是未定义的。它不会从函数返回数据。
(以防万一你们问的“PRODID”在控制器的参数是好的,和检索,从另一个控制器)
我怎么能解决这个问题? :(
在此先感谢。
你的工厂函数不返回任何东西,这就是为什么你得到'undefined' ...返回请求并使用控制器中的'then'来设置scope属性。成功之内的“回报”不会做任何事 – charlietfl
@charlietfl你能回答下面的一个小例子,然后我可以打勾你作为正确的答案,如果这个工程?谢谢 ! :) – user3078876