Solved

Share code across controller Angularjs

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to dynamically set the form action using jQuery.

739 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