python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
Can I make a function available in every controller in AngularJs?

Can I make a function available in every controller in AngularJs?

Global Functions in AngularJs

Hello everyone, I am going to share the code sample to create the global functions available in every controller.

Here in AngularJs, have two options for creating global functions. i.e.

1. Define a service for global function.
2. Using  run method for root scope.

Here, I am going to use the  $rootScope and app.run() on initialization in AngularJs as given below example.

The live demo link   http://embed.plnkr.co/lQfOiX/preview

//This is global functions for every controller.
app.run(function ($rootScope) {
    $rootScope.myGlobalFunction = function () {
     };
});

The AngularJs code-sample

var app = angular.module('myApp', []);

//This is global functions for every controller.
app.run(function ($rootScope, $window) {
    $rootScope.myGlobalFunction = function () {
        //TODO : your custom code here.
        $window.alert('I am global function. :)');
        console.log("I am global function. :)");
    };
});

//This is controller for accessing to global functions.
app.controller('MainCtrl', function ($scope) {
    //TODO: your controller code here.
});

The HTML code-sample

<div ng-app="myApp" ng-controller="MainCtrl">
    <div ng-init="myGlobalFunction()">
        <button ng-click="myGlobalFunction()">Click and call global function</button>
    </div>
</div>

The Live demo code (HTML + AngularJs) as given below.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Global Functions in AngularJs</title>
    <link rel="stylesheet" href="style.css" />
    <script src="https://code.angularjs.org/1.4.3/angular.js"></script>
    <script>
        var app = angular.module('myApp', []);

        //This is global functions for every controller.
        app.run(function ($rootScope, $window) {
            $rootScope.myGlobalFunction = function () {
                //TODO : your custom code here.
                $window.alert('I am global function. :)');
                console.log("I am global function. :)");
            };
        });

        //This is controller for accessing to global functions.
        app.controller('MainCtrl', function ($scope) {
            //TODO: your controller code here.
        });
    </script>
</head>
<body ng-app="myApp" ng-controller="MainCtrl">
    <p class="txt">Global Functions in AngularJs</p>
    <div ng-init="myGlobalFunction()">
        <p>
            <a href="http://code-sample.com/">Click for know about me. :)</a>
        </p>
        <button class="btn" ng-click="myGlobalFunction()">Click and call global function</button>
    </div>
</body>
</html>

The output : go to link  http://embed.plnkr.co/lQfOiX/preview

The output look like below image.




ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions