javascript - 如何在 angular.js 中进行自动数据更新?

标签 javascript jquery angularjs

我需要在服务器创建新数据时更新 View 吗? 如何正确执行此操作?

我的 Controller

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 中的服务是对象。因此,创建一个返回 promise 的简单方法,以在 Controller 中对其进行管理。

Controller

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

}]);

关于javascript - 如何在 angular.js 中进行自动数据更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32527267/

相关文章:

javascript - 将 html 文本框值传递给 insidehtml 文本框

javascript - react : Add jquery needed by another library

javascript - 如何在 Google map 之外点击图钉上的位置详细信息

javascript - AngularJS 查询参数

javascript - 从另一个函数调用 jQuery 的 DOM 就绪函数

javascript - 登录后 Facebook api 不会重新加载

javascript - Apps 脚本错误 - 排序范围必须包括表单上的所有列

JavaScript:我如何动态地 "filter"我的对象

javascript - 如何检测设备是否支持鼠标?

javascript - 未定义的 ng-bind 是否比 ng-bind 和 ng-show 更昂贵?