控制器没有从AngularJS的工厂获得$ http返回数据

时间:2017-07-28 00:27:03

标签: javascript html angularjs controller factory

尝试使用此代码返回$ scope.top中的http返回数据,并使用页面上的数据使用{{top}}填充元素。

我尝试的所有变体都会导致空的$ scope.top或$ scope.top数据到达时间太晚而无法在页面上使用。

最新尝试:

var app = angular.module("it", []);
    app.factory('dataProvider', function($http){
        return {
            getTop: function(){
                return $http.get("db.php");
            }
        }
    });

    app.controller('topList', function($log, $scope, dataProvider){
        $scope.getTop = function(){
            var promise = dataProvider.getTop();
            promise.then(
                function(data){ 
                    $scope.top = data.data;
                },
                function(errorData){
                    $log.error('failure loading', errorData);
                });
        }

    });

我做错了什么?

0 个答案:

没有答案
相关问题