2016-04-11 12 views
1

形成我所知道的函數應該從上到下。但在這段代碼中,手錶首先會比getOrders函數執行。任何人都有讓getOrders函數首先執行的解決方案?

$scope.getOrders=function() { 
     OrderService.getOrders(1) 
     .success(function (ords) { 
      if (angular.isArray(ords)) { 
       $scope.Orders = ords; 
       console.log("testing2"); 
       console.log("testing 3"+$scope.Orders); 
      } else { 
       $scope.Orders = [ords]; 
       console.log("testing"); 
      } 

      $scope.dateT = true; 
      $scope.StatusEnum = { 10: "Pending", 20: "Processing", 30: "Ready to Shipping", 40: "Cancelled" }; 

     }).error(function (error) { 
      $scope.status = 'Unable to load customer data: ' + error.message; 
      console.log($scope.status); 
     }); 
    } 
//Here edited, I got add in but still cant 
$scope.getOrders(); 
$scope.$watch('currentPage + numPerPage', function() { 

      var begin = (($scope.currentPage - 1) * $scope.numPerPage), end = begin + $scope.numPerPage; 
      //$scope.filteredTodos = $scope.Orders.slice(begin, end); 
      console.log("testing" + $scope.Orders); 
     },1); 

但我在控制檯看到的結果是這樣的 enter image description here

+0

getOrders是如何執行的? – dfsq

+0

我在$ watch之前執行了它,但它仍然是一樣的。 $ scope.getOrders(); $ watch('currentPage + numPerPage',function(){}); 不知道如何ng-bind。 – kyorilys

+0

當然,這是預期的行爲,沒有錯。只需檢查手錶中的數據是否存在。 – dfsq

回答

0

您可以比較新的價值舊值,如果是不同的,只有它被執行,就像這樣:

$scope.$watch('your variable', function(newValue, oldValue){ 
    if(newValue != oldValue){ 
     //execute your code here 
    } 
    }, true); 
+0

但我希望它稍後執行。意思是在執行watch之前,讓getOrders()首先執行 – kyorilys

+1

爲什麼不更新currentPage + numPerPage到'成功'(所以手錶要執行)函數並創建一個新變量,如$ scope.total = currentPage + numPerPage;並將「總計」放在手錶內? –

0

您錯誤地記錄它,你testing2testing 3日誌調用不是從getOrders功能,但從成功回調。此外,您並未記錄$watch函數,但您記錄的是在currentPage + numPerPage表達式中的值更改時執行的回調。 getOrders$watch函數按照您預期的方式執行,但回調函數不按該順序執行。

如果你想爲一些代碼,當訂單返回,你應該在success回調執行它,這樣來執行:

$scope.getOrders=function() { 
     OrderService.getOrders(1) 
     .success(function (ords) { 
      if (angular.isArray(ords)) { 
       $scope.Orders = ords; 
       console.log("testing2"); 
       console.log("testing 3"+$scope.Orders); 
      } else { 
       $scope.Orders = [ords]; 
       console.log("testing"); 
      } 

      $scope.dateT = true; 
      $scope.StatusEnum = { 10: "Pending", 20: "Processing", 30: "Ready to Shipping", 40: "Cancelled" }; 

      // execute some code here (I'm making it up) 
      $scope.currentPage = ords.currentPage; 
      $scope.numPerPage = ords.numPerPage; 
      // this will also trigger the watch you registered 
      // or you can do what you do in watch callback here 

     }).error(function (error) { 
      $scope.status = 'Unable to load customer data: ' + error.message; 
      console.log($scope.status); 
     }); 
    }; 
0

THX到@theo關於成功,我只是把$範圍$ watch功能進入成功。然後它工作。

$scope.getOrders=function() { 
      OrderService.getOrders(1) 
      .success(function (ords) { 
       if (angular.isArray(ords)) { 
        $scope.Orders = ords; 
        console.log("testing2"); 
        console.log("testing 3"+$scope.Orders); 
       } else { 
        $scope.Orders = [ords]; 
        console.log("testing"); 
       } 

       $scope.dateT = true; 
       $scope.StatusEnum = { 10: "Pending", 20: "Processing", 30: "Ready to Shipping", 40: "Cancelled" }; 

       //Move the watch inside the success 
       $scope.$watch('currentPage + numPerPage', function() { 
        var begin = (($scope.currentPage - 1) * $scope.numPerPage), end = begin + $scope.numPerPage; 
        $scope.filteredTodos = $scope.Orders.slice(begin, end); 
        console.log("testing" + $scope.Orders); 
       }); 
      }).error(function (error) { 
       $scope.status = 'Unable to load customer data: ' + error.message; 
       console.log($scope.status); 
      }); 
     } 
+0

如果你兩次調用'getOrders'函數會發生什麼? – stjepano

相關問題