Solved

Share code across controller Angularjs

Posted on 2015-01-05
4
105 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now