控制器方法不返回值来扩展控制器

时间:2017-04-17 16:02:30

标签: angularjs ajax controllers

我有一个扩展另一个控制器的控制器:

我需要将第二个控制器方法的结果接收到第一个控制器的变量

第一控制器:

angular.module('Gestios').controller('EmpresasControllerList', function($scope, $controller ) {
    angular.extend(this, $controller('AppController', {
        $scope: $scope
    }));

    $scope.App = $scope.runApp('empresas', true);

    console.log($scope.App);
});

第二名管制员:

app.controller('AppController', function ($scope, $controller, AppFactory) {
    $scope.runApp = function (AppPermalink, GetResults, Filters, Callback) {
        if(!angular.isDefined(Filters)){Filters = $scope.Filters;}

        var App = {
            Load : {
                busy: false,
                page: 1
            },
            Info: {},
            Results: []
        };

        AppFactory.Applications(AppPermalink).then(function (data) {
            App.Info = data.data[0];

            if(GetResults){
                AppFactory.Items(AppPermalink, Filters).then(function (data) {
                    App.Results = {
                        list: data.data.data,
                        count: data.data.total
                    };

                    if(typeof(Callback) == "function"){Callback(true, {results: App.Results.list, count: App.Results.count});}

                    return App;
                },function () {
                    App.Results = {
                        list: [],
                        count: 0
                    };

                    if(typeof(Callback) == "function"){Callback(false, {results: App.Results.list, count: App.Results.count});}

                    return App;
                });
            }
        },function (error) {
            swal("Algo ha ido mal", error.error, "error");
        });
    };
});




});

http呼叫接收数据 ¿异步http调用可能导致问题?

1 个答案:

答案 0 :(得分:0)

使用我的回调解决了它:p

第一控制器:

angular.module('Gestios').controller('EmpresasControllerList', function($scope, $controller ) {
    angular.extend(this, $controller('AppController', {
        $scope: $scope
    }));

    $scope.runApp('empresas', true, false, function(Result, App){
        if(Result){
            $scope.App = App;
        }
    });
});

第二个控制器的方法(AppController):

$scope.runApp = function (AppPermalink, GetResults, Filters, Callback) {
    if(!angular.isDefined(Filters)){Filters = $scope.Filters;}

    var App = {
        Load : {
            busy: false,
            page: 1
        },
        Info: {},
        Results: []
    };

    AppFactory.Applications(AppPermalink).then(function (data) {
        App.Info = data.data[0];

        if(GetResults){
            AppFactory.Items(AppPermalink, Filters).then(function (data) {
                App.Results = {
                    list: data.data.data,
                    count: data.data.total
                };

                if(typeof(Callback) == "function"){Callback(true, App);}
            },function () {
                App.Results = {
                    list: [],
                    count: 0
                };

                if(typeof(Callback) == "function"){Callback(false, App);}
            });
        }
    },function (error) {
        swal("Algo ha ido mal", error.error, "error");
    });
};