SEARCH BY

All

  • All
  • Java8
  • Spring

ng-app in AngularJS

Post a Comment

Introduction

Here iam trying to explain what is the use of ng-app( one of the AngularJS directive ).

In AngularJS every application must be intialized with the binding of ng-app directive. ng-app is adding as an attribute to the DOM of HTML page. Generaly ng-app will be adding to html tag or body tag, to let that DOM is binding to AngularJS module.

ng-app is a root for AngularJS application. With ng-app we can initialize AngularJS to HTMl page/application

<html>

<head>
</head>

<body ng-app="cookiestore_app">
    <div ng-controller="welcomeBlock">
        {{message}}
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
    <script src="./app/app.js"></script>
</body>

</html>

In the above code in line 6 we have added an attribute ng-app to body tab with the value as cookiestore_app, that means AngulaJS intiated here.

We bind AngularJS controller to the DOM div where controller named as welcomeBlack assighned as a value to one more AngularJS directive ng-controller

var appRoot=angular.module("cookiestore_app",[]);
(function() {
    'use strict';

    appRoot.controller('welcomeBlock', welcomeBlock);

    welcomeBlock.$inject = ['$scope'];
    function welcomeBlock($scope) {
        var vm = this;
        $scope.message="Welcome to my site";

        activate();

        ////////////////

        function activate() { }
    }
})();


In line 1 from the above code ( app.js ) we have initiated cookiestore_app as an AngularJS module, by passing ng-app value as a first parameter to function angular.module() and dependencies can be added in array, but here in this example no dependecies are passing so that we are passing empty array as a second parameter.

OutputOutput
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox