javascript - 为什么传递给 Controller ​​函数后值是 "Undefined"

标签 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>

Controller 内我的http:

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

产品低于响应:

[
{
    "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
}
]   

最佳答案

原因是: 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 ... ');
        });

关于javascript - 为什么传递给 Controller ​​函数后值是 "Undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33699621/

相关文章:

javascript - AngularJs - 将 ng-controller 与 ng-include 一起使用

javascript - 通过filter返回一个数组

javascript - 使用 jQuery 为悬停时的 SVG 颜色和缩放设置动画

javascript - 在使用 Html/WinJS 开发的 Metro 应用程序中实现日志记录

javascript - PHP持久登录不用担心窃取cookie

angularjs - 手动安装 Selenium Server 以使用古巴的 Protractor (AngularJS)

angularjs - Angular ui-router $state 更改 URL

javascript - 从变量值动态设置 ng-controller

javascript - AngularJS - 参数 'MenuCtrl' 不是函数,未定义

javascript - 弹出窗口出现在其前一条语句执行之前