Implement a delay on $scope.$watch

前端 未结 5 1617
醉话见心
醉话见心 2021-02-07 03:33

I was wondering whether or not it is possible to implement a slight delay on $scope.$watch. I have the following which queries the server, so I\'d like to implement a slight del

5条回答
  •  花落未央
    2021-02-07 03:53

    You can use the current value of query to decide when you want to fire the call:

    $scope.$watch("query", function (value) {
    
        //implement rule here for value
        //example value is at least 3 characters
        if (value && value.length > 3) {
    
            $scope.loading = true;
            returnFactory.query($scope.query).then(function (returns) {
                $scope.returns = returns;
                $scope.loading = false;
            });
        }
    });
    

提交回复
热议问题