Solved

Share code across controller Angularjs

Posted on 2015-01-05
4
111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

617 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