1

I have an angular controller called SubmissionTreeController and it has update_dashboard() function which refreshes the ui every minute.

My goal is to refresh the ui on successful post request from a different controller.

How do I make this function available in other controllers?

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

Edit:

What I am trying to do:

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

What I am getting: Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- updateDashboardService

Edit 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
  • 3
    create a factory method,so it can be used across controllers Commented Jun 18, 2014 at 9:03

1 Answer 1

2

As @Gopesh says create a factory method, or, you can do something like this in SubmissionTreeController:

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

And in your other controller:

    $http.post(url, $scope.selected_items).success(function(data) {
        $scope.$emit("event:updateDashboard");
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Could you check at my code that I've provided? It does not seem to work
I've added a working example in my first post. I'll take a look into scope.emit because it might be way cleaner way in usage of functions in different parts of application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.