传递给控制器​​功能后,为什么值为“未定义”

时间:2015-11-13 18:35:33

标签: javascript angularjs ng-controller

我不确定为什么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
}
]   

1 个答案:

答案 0 :(得分:3)

原因是: 在ajax返回之前调用onSizeSelected

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