2013-12-11 30 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

相關問題