Solved

Share code across controller Angularjs

Posted on 2015-01-05
4
108 Views
Last Modified: 2015-01-12
I am adding this piece of code across 10 controllers which is not good way as I am repeating them .

Is there a way I can write this  one service and inject in the controller?

I dont know how to return the code?Let me know a good way

$idle.watch();
    $scope.started = true;

function closeModals() {
        if ($scope.warning) {
            $scope.warning.close();
            $scope.warning = null;
        }

        if ($scope.timedout) {
            $scope.timedout.close();
            $scope.timedout = null;
           
        }
    }

    $scope.$on('$idleTimeout', function () {
        $location.path('/');
        closeModals();
        $scope.timedout = $modal.open({
            templateUrl: 'timedout-dialog.html',
            windowClass: 'modal-danger'
        });
    });

    $scope.$on('$idleStart', function () {

        closeModals();

        $scope.warning = $modal.open({
            templateUrl: 'warning-dialog.html',
            windowClass: 'modal-danger'
        });
        //$location.path('/');
    });

    $scope.$on('$idleEnd', function () {
        closeModals();
    });
0
Comment
Question by:rbhargaw
  • 3
4 Comments
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 40533039
HI,

Best thing will be to use some common Module or Library and then use that Library functions from all places required.

for example you can use global service or common service in AngularJS.

Thanks
0
 

Author Comment

by:rbhargaw
ID: 40533637
I did try to encapsulate the code inside a service but I am not sure  what to return. e.g. The code which I have added above have three $scope.on. So does this go inside the service and how do I call all these code inside controller?
0
 

Accepted Solution

by:
rbhargaw earned 0 total points
ID: 40536117
I have found an alternate solution for my problem and hence this question is now of no use. so closing the ticket
0
 

Author Closing Comment

by:rbhargaw
ID: 40543975
Found an alternate solution for the problem
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question