Angularjs没有更新$ http.get内的数据

时间:2015-06-18 07:13:40

标签: javascript angularjs

这是我的代码:

$scope.t = ["lapi", "laptop"];

$scope.$watch('search', function(newval, oldval){

$scope.alldata = [];

$http.get('http://localhost/serve/?p=12345').

    then(function(data){

        var x = Math.floor((Math.random() * 2));

        $scope.alldata.push($scope.t[x])

    },

    function(){

        console.log("error")

    });

    return $scope.alldata;

});

每次用之前的结果更新。

如果我在推送后执行$ scope。$ apply(),

显示错误:[$ rootScope:inprog]

我不知道如何更新这些数据。

<!DOCTYPE>
<html ng-app="test">
  <head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.0/ui-bootstrap-tpls.min.js"></script>
    <script src="sys.js"></script>
  </head>
  <body ng-controller="main">
    <p>Angularjs Test</p>
    <input type="text" ng-model="search" typeahead="data for data in alldata | filter:$viewValue | limitTo:10"autofocus>
  </body>
</html>

var app=angular.module('test', ["ui.bootstrap"]);

app.controller("main", function($scope, $http, $timeout, $q){
    console.log("main");
    var timer = false;
    $scope.search = "";
    $scope.t = ["vijay", "vijaypal"];
    $scope.alldata = [];
    $scope.getLog = function(){
        console.log("rootscope called");
    };
    $scope.$watch('search', function(newval, oldval){
        
        if(timer) {
            $timeout.cancel(timer);
        }
        timer = $timeout(function(){
            $scope.alldata = [];
            var rd='&_rd='+new Date().getTime();
            var deferred = $q.defer();
            $http.get("http://rec.cloudinfra.in/rec_serve_property/?p=3903405"+rd).
            then( function(data) {
                var x = Math.floor((Math.random() * 2));
                    $scope.alldata.push($scope.t[x]);
                    deferred.resolve($scope.alldata)
                    console.log("start:", $scope.alldata);
                    return deferred.promise;
            },
            function (){
                console.log("error");
            });
        }, 100);
        console.log("end:", $scope.alldata);
        
    });
});

检查控制台日志,您将遇到问题

2 个答案:

答案 0 :(得分:0)

看起来像缓存问题,并尝试将请求更改为

var rd='&_rd='+new Date().getTime();

$http.get('http://localhost/serve/?p=12345'+rd)

所以每次都可以有不同的请求

答案 1 :(得分:0)

语法是 $ http.get()。success(function(){})。error(function(){}); 据我所知。

$scope.t = ["lapi", "laptop"];

$scope.$watch('search', function(newval, oldval){

$scope.alldata = [];

$http.get('http://localhost/serve/?p=12345').success(function($data) {
   //$data is the data stripped from the response! unlike XMLHttpRequest
   var x = Math.ceil(Math.random() * 2) - 1; //This wasn't an integer
   $scope.alldata.push($scope.t[x]);
   //no need to return
}).error(function() {
   console.log('error');
});