Angular.js - 在其他控制器中可用的制作功能

时间:2014-06-18 09:00:56

标签: javascript angularjs

我有一个名为SubmissionTreeController的角度控制器,它有update_dashboard()函数,每分钟刷新一次ui。

我的目标是从不同的控制器刷新成功发布请求的ui。

如何在其他控制器中使用此功能?

var module = angular.module("submissionDashboard", ['ui.tree', 'ngCookies', 'ui.bootstrap',]);

module.controller("SubmissionTreeController", ["$scope", "$http", "$modal", 
function($scope, $http, $modal) {
    $scope.selected_items = {};

    var update_dashboard = function() {
        var url = Django.url('submission:active_list_ajax', {
            site : site
        });

        $http.get(url).success(function(data) {
            $scope.list = data.results;
        });
    };

    update_dashboard();
    $scope.closeTask = function(scope) {
        var modalInstance = $modal.open({
            templateUrl: 'modal_close_submission_renderer.html',
            controller: 'ModalCloseSubmissionController',
            resolve: {
                items: function () {
                    return $scope.selected_items;
                }} 
        });
    };
}]);

module.controller('ModalCloseSubmissionController', ['$scope', '$modalInstance', '$http', 'items',  function ($scope, $modalInstance, $http, items) {
    $scope.items = items;

    $scope.selected = {
        item: 1,
        text: ''
    };

    $scope.ok = function () {
        var val = $scope.selected.item;
        if (val === 1) {
            var url = Django.url('submission:close-notify', {
                site : site
            });
            $http.post(url, $scope.selected_items).success(function(data) {
                update_dashboard();
            });
        } else if (val === 2) {
            var url = Django.url('submission:close', {
                site : site
            });

            $http.post(url, $scope.selected_items).success(function(data) {
                update_dashboard();
            });
        } else if (val === 3) {
            var url = Django.url('submission:cancel', {
                site : site
            });

            $http.post(url, $scope.selected_items).success(function(data) {
                update_dashboard();
            });
        };

        $modalInstance.close($scope.selected.item);
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

编辑:

我想做什么:

module.service('updateDashboardService', function($scope, $http){
    this.update_dashboard = function() {
        $scope = $scope; 
        var url = Django.url('submission:active_list_ajax', {
            site : site
        });

        $http.get(url).success(function(data) {
            $scope.list = data.results;
        });
    };
});

module.controller("SubmissionTreeController", ["$scope", "$http", "$modal", "updateDashboardService", function($scope, $http, $modal, updateDashboardService) {
    $scope.selected_items = {};

    updateDashboardService.update_dashboard();

    var timer = setInterval(function() {
        $scope.$apply(updateDashboardService.update_dashboard($scope, $http));
    }, 1000 * 60);

我得到的是: Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- updateDashboardService

编辑2:

module.service('updateDashboardService', function($rootScope, $http){
    this.update_dashboard = function() {
        var url = Django.url('submission:active_list_ajax', {
            site : site
        });

        $http.get(url).success(function(data) {
            $rootScope.list = data.results;
        });
    };
});

1 个答案:

答案 0 :(得分:2)

正如@Gopesh所说创建一个工厂方法,或者,你可以在SubmissionTreeController中做这样的事情:

    $scope.$on("event:updateDashboard", function(){ update_dashboard() });

在你的另一个控制器中:

    $http.post(url, $scope.selected_items).success(function(data) {
        $scope.$emit("event:updateDashboard");
    });