Angular跳过get方法调用Web服务

时间:2015-07-03 06:59:21

标签: angularjs

    var l = $scope.PendingKOTitems.length,
        k = 0;
    for (k = 0; k < l; k = k + 1) {
        sItemCode = $scope.PendingKOTitems[k]["sItemCode"]
        sOutletCode = $scope.selectedOutlet.sOutletCode;
        sFinal = sOutletCode + "/" + sSubCatgCode

        $http.get(sServiceURL + 'ItemMst/' + sFinal).then(function (resp) {
            $scope.Items = resp.data;
            iindexItem = getIndexOf($scope.Items, sItemCode, "sItemCode");
        }, function (err) {
            console.error('ERR', err);
            // err.status will contain the status code
        })
        sCheckValue = $scope.CheckboxSelection.value;

         $scope.invoice.items.push({
            KOTNo: $scope.KOTNumValue,
            ActiveMode: $scope.lActiveMode,
            SubCatgCode: sSubCatgCode,
            SubCatgDesc: sSubCatgDesc,
            indexSubCatg: iindexSubCatg,
            ItemCode: sItemCode,
            ItemDesc: sItemDesc,
            indexItem: iindexItem,
            Qty: sQty,
            OutletCode: sOutletCode,
            BillType: sBillType,
            MemberCode: sMemberCode,
            RoomNum: sRoomNum,
            BookingCode: dBookingCode,
            TableCode: sTableCode,
            WaiterCode: sWaiterCode,
            UserID: sUserID
        });
    }

for循环中的get方法没有触发,执行for循环后它将触发。

我希望根据项子类别获取负载选择的'iindexItem'值。但是在'push'操作执行后获取方法。所以我无法获得'iindexItem'的价值。这是我的问题。

1 个答案:

答案 0 :(得分:0)

我试过这个,这似乎对我有用 -

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular.js"></script>
</head>
<body ng-app="app">
 <div ng-controller="ctrl">

 <h1>This is a Heading</h1>
 <p>This is a paragraph.</p>
</div>
<script type="text/javascript">
  var angularapp = angular.module("app",[]);

  angularapp.controller('ctrl', ['$scope', '$window', '$http', '$filter', '$timeout', MyCtrl]);

  function MyCtrl($scope, $window, $http, $filter, $timeout){

  for(var i=0; i<3; i++){
   $http.get("http://httpbin.org/get").then(function (resp) {
     debugger;
     $scope.Items = resp.data; // here I set my breakpoint
     console.log(i);
     console.log(resp.data);
 }, function (err) {

    console.error('ERR', err);
    // err.status will contain the status code
  });
 }

}
</script>
</body>
</html>     

for循环中的每次迭代都会调用调试器。