2013-12-11 32 views
-1

我得到这个角度厂:为什么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”在控制器的参数是好的,和检索,从另一个控制器)

我怎么能解决这个问题? :(

在此先感谢。

+0

你的工厂函数不返回任何东西,这就是为什么你得到'undefined' ...返回请求并使用控制器中的'then'来设置scope属性。成功之内的“回报”不会做任何事 – charlietfl

+0

@charlietfl你能回答下面的一个小例子,然后我可以打勾你作为正确的答案,如果这个工程?谢谢 ! :) – user3078876

回答

1

我已经习惯了解决这一问题的模式是成功&错误回调函数传递到工厂......是这样的:

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'); 
    }; 
}); 

通过以这种方式代替,您可以访问控制器中的作用域&可以根据需要对返回的数据执行任何操作。

+1

谢谢!这一工作! :)很好的回调实现!不知道!即时通讯有点菜鸟。 – user3078876

1

这就是我做的。我使用的,而不是$ HTTP $ resournce,但它应该足以让你去。你甚至可能要使用$资源,因为它内置的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调用或在其他地方调用它。

希望这回答你的问题。

+0

感谢您的帮助!希望我能打你的答案也是一个很好的答案! – user3078876

+0

没问题。我想我是在与另一个人同时打字的。干杯。 – Darryl

相关问题