Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在angular.js中自动更新数据?_Javascript_Jquery_Angularjs - Fatal编程技术网

Javascript 如何在angular.js中自动更新数据?

Javascript 如何在angular.js中自动更新数据?,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我需要在服务器上创建新数据时更新视图? 如何做到这一点 我的控制器 app.controller('MainController', ['$scope', 'games', function($scope, games) { games.success(function(data) { console.log(data[0]); $scope.games = data[0]; }); }]); app.factory('games', ['$http', function($

我需要在服务器上创建新数据时更新视图? 如何做到这一点

我的控制器

app.controller('MainController', ['$scope', 'games', function($scope, games) {
games.success(function(data) {
    console.log(data[0]);
    $scope.games = data[0];
}); 
}]);
app.factory('games', ['$http', function($http) {


return $http.get('./game')
     .success(function(data) {
       return data;
     })
     .error(function(data) {
       return data;
     });
}]);
我的工厂

app.controller('MainController', ['$scope', 'games', function($scope, games) {
games.success(function(data) {
    console.log(data[0]);
    $scope.games = data[0];
}); 
}]);
app.factory('games', ['$http', function($http) {


return $http.get('./game')
     .success(function(data) {
       return data;
     })
     .error(function(data) {
       return data;
     });
}]);

记住,Angular中的服务是对象。因此,创建一个返回承诺的简单方法,在控制器中管理它

控制器

app.controller('MainController', ['$scope', 'games', function($scope, games) {

        games.get().then(function(data) {
            console.log(data[0]);
            $scope.games = data[0];
        });

    }]);
服务

app.service('games', ['$http', function($http) {

    this.get = function() {
        return $http.get('./game');
    };

}]);

如果不希望使用WebSocket,可以使用
$timeout

$timeout(function(){
  games.success(function(data) {
    console.log(data[0]);
    $scope.games = data[0];
 }); 
},1000);
更新:抱歉,应该是$interval

$interval(function(){
  games.success(function(data) {
    console.log(data[0]);
    $scope.games = data[0];
 }); 
},1000);
更新:如何使用factory执行此操作

app.factory('games', ['$http', function($http) {
    return {
        getGame: function() {
            return $http.get('./game');
        }
    }
}]);
现在在你的控制器里

app.controller('MainController', ['$scope', 'games', function($scope, games) {
    games.getGame().success(function(data) {
        $scope.games = data[0];
    });
}]);

如果后端需要能够通知前端而不需要像在普通RESTful请求中那样被查询,那么可能需要查看web套接字。web套接字就是答案!使用$timeout时遇到了什么问题?同样的结果,我需要刷新页面以查看新数据。我以为您想实时更改数据,但请让我输入如何使用factory执行相同操作的代码。我将$interval添加到控制器中的get方法。提供程序“games”必须从$get factory方法返回值。抱歉,你是对的。我忘了把“工厂”改成“服务”。让我知道它是否有效!我已经改变了答案。过来看。