2015-11-13 25 views
0

后是 “未定义” 我不知道为什么console.log说不确定为什么值传递给控制器​​功能

$scope.onSizeSelected = function(productId, sizeQtyPrice){ 
    console.log('size selected ...' + sizeQtyPrice); 
    $scope.updateSelectedProductBySizeSelected(productId ,sizeQtyPrice); 
}; 

我的HTML div标签:

<div ng-show="product[0].sizeQtyPrice[0].size > 0" ng-init="onSizeSelected(product[0].id, product[0].sizeQtyPrice[0])" >Select a Size</div> 

http内部控制器:

$http.get("/get_product_details/?prod_id=1") 
     .success(function (response) { 
      $scope.product = response; 
     }).error(function(){ 
      console.log('Error happened ... '); 
     }); 

product得到低于response

[ 
{ 
    "selectedQtyOptions": [], 
    "selectedSize": "", 
    "description": "taxiing", 
    "selectedQty": "1", 
    "title": "nationally", 
    "brand": "Brand2", 
    "product_identifier_type": "SKU", 
    "images": [ 
     { 
      "image0": "/media/products/bb61e8ae422b736ff6c1b9562cbde883.jpg" 
     } 
    ], 
    "sizeQtyPrice": [ 
     { 
      "discountAttributes": "Jung fords redskin richest pearl paperweight careen confides backstage gushing", 
      "measureUnit": "mm", 
      "discountPercent": 5, 
      "mrp": 8269, 
      "qty": 2, 
      "size": 62 
     }, 
     { 
      "discountAttributes": "snitched wisps unambiguously harshest clothed famished spec triathlon Ethelred addicts", 
      "measureUnit": "Kg", 
      "discountPercent": 10, 
      "mrp": 5644, 
      "qty": 6, 
      "size": 92 
     }, 
     { 
      "discountAttributes": "committal forming Welsh mawkishly swapped merchandize brawn demises emailed UCLA", 
      "measureUnit": "Kg", 
      "discountPercent": 3, 
      "mrp": 7106, 
      "qty": 5, 
      "size": 32 
     } 
    ], 
    "product_identifier": "8e4e9389-6c46-4dc8-8716-0c7d2e580d3e", 
    "id": 1 
} 
] 

回答

3

的原因是: onSizeSelected获取AJAX返回之前调用。

$http.get("/get_product_details/?prod_id=1") 
     .success(function (response) { 
      $scope.product = response; 
      //<------------ set here the data 
     }).error(function(){ 
      console.log('Error happened ... '); 
     }); 
+0

谢谢!这工作,但你可以帮助我在这里使用'承诺'。我读abt它,但作为一个新手,我不知道如何在这里使用它(我的意思是ng-init)。请你帮忙 –