我的ng模型没有't更新我的$scope值

My ng-model didn't update my $scope value?

本文关键字:我的 更新 scope ng 模型      更新时间:2023-09-26

我正在尝试为数字创建一个输入字段,该字段将使用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>

但如果我运行我的应用程序,它既不会在我更改号码时更新我的表格,也不会返回控制台"passed!"消息。

我做错了什么?我该怎么解决?

您应该为此使用AngularJS过滤器https://docs.angularjs.org/api/ng/filter/filter

否则,您需要使用$scope$申请http://jimhoskins.com/2012/12/17/angularjs-and-apply.html

确保在您的视图中并且您已经指定了控制器:

ng-controller="CarsByRatingCtrl"

您需要对scope变量使用一个监视来自动对该变量运行更新。

$scope.$watch('minReviewNum', function() { //... });

使用这个:

<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();
});