角度服务未返回数据

angular service not returning data

本文关键字:返回 数据 服务      更新时间:2023-09-26

我是angular和web项目的新手,我第一次实现服务,我遇到了问题:

var app = angular.module("mdmapp", ['ui.router']);
app.config(function ($stateProvider) {
    $stateProvider
        .state('creafund', {
            url: '/creafund',
            templateUrl: '/mdm/html/fundtype.html'
        })
        .state('creareceipt', {
            url: '/creareceipt',
            templateUrl: '/mdm/html/receipttype.html'
        })
        .state('fundentry', {
            url: '/fundentry',
            templateUrl: '/mdm/html/fundentry.html'
        })
        .state('payentry', {
            url: '/payentry',
            templateUrl: '/mdm/html/payentry.html'
        })
        .state('reports', {
            url: '/reports',
            templateUrl: '/mdm/html/reports.html'
        });
});
app.service('DataServer', function ($rootScope, $http) {
    //this function is to get the list of fund types.
    this.GetFundType = function () {
        $http.get('/mdm/server/app.php/GetFundType')
            .then(function (response) {
                return response;
            });
    };
});
//controllers for the various pages and sections.
app.controller('mainctrl', function ($scope, DataServer) {
    $scope.FundTypeList = DataServer.GetFundType();
});
app.controller('ftctrl', function ($scope, $http, DataServer) {
    $scope.SaveFundType = function () {
        var data = {desc: $scope.ftdesc};
        $http.post('/mdm/server/app.php/FundTypeCreate', data).success(
            function (data, status, headers) {
                $scope.ftdesc = null;
                $scope.FundTypeList = DataServer.GetFundType();
            }
        );
    };
});

在上面提到的代码中,我无法从"dataever"服务获取数据,它正在从rest api获取json响应。。。将呈现一个表。

您需要等待控制器中的promise,如下所示:

function Factory($http) {
    var service = {
      GetFundType: GetFundType
    };
    return service;
    function GetFundType() {
        return $http.get('/mdm/server/app.php/GetFundType');
    }
}
function Controller1($scope,Factory) {
  Factory.GetFundType().then(function(response) {
    $scope.FundTypeList = response.data;
  });
}