Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Share code across controller Angularjs

Posted on 2015-01-05
4
Medium Priority
?
114 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

916 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