2014-09-05 72 views
0

我正在嘗試爲使用ng-model自動更新我的表格的數字創建一個輸入字段。

這裏是我的控制器:

carApp.controller("CarsByRatingCtrl", function($scope, getAllCars){ 
    getAllCars.get().success(function(data){ 
     $scope.carList = data; 
     $scope.minReviewNum = 0; 

     if ($scope.minReviewNum > 0){ 
      console.log("passed!") 
      for(var i=0;i<$scope.carList.length;i++){ 
       if ($scope.carList[i]["review"]<$scope.minReviewNum){ 
        $scope.carList.splice(i, 1); 
       } 
      } 
     } 
    }); 
}); 

這裏是一部分,如果我輸入字段:

<input type="number" min="0" step="10" value="0" name="num" ng-model="minReviewNum"></input> 

但是,如果我跑我的應用程序,因爲我換號,也不回它既不更新我的表到控制檯「通過!」信息。

我做了什麼錯,我該如何解決?

回答

-1

使用這樣的:

<input type="number" min="0" step="10" value="0" name="num" ng-model="minReviewNum" ng-change="update()"></input> 

,並作爲控制器使用本:

carApp.controller("CarsByRatingCtrl", function($scope, getAllCars){ 
$scope.update=function(){ 
    getAllCars.get().success(function(data){ 
     $scope.carList = data; 
     $scope.minReviewNum = 0; 

     if ($scope.minReviewNum > 0){ 
      console.log("passed!") 
      for(var i=0;i<$scope.carList.length;i++){ 
       if ($scope.carList[i]["review"]<$scope.minReviewNum){ 
        $scope.carList.splice(i, 1); 
       } 
      } 
     } 
    }); 
} 
$scope.update(); 
}); 
0

確保在您的視圖中並且您指定了控制器:

ng-controller="CarsByRatingCtrl" 

您需要對範圍變量使用watch來自動在該變量上運行更新。

$scope.$watch('minReviewNum', function() { //... }); 
相關問題