AngularJs controller example

The AngularJs controller is an object that is created by using JavaScript constructor.

The controller is handling the DOM elements with the help of using ng-controller directive and angular scope.

What Is Scope?  The scope is an object that works between the controller and DOM and proved the link between the controller and view.

The controller are used to initialize the $scope objects and add the $scope behavior also.


The controller are contains the business logic only and not used for DOM manipulation.

The Syntax of controller

var app = angular.module('angularApp', []);
app.controller('angularCtrl', function ($scope) { });

The full live Example code as given below
<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('angularApp', []);
        app.controller('angularCtrl', function ($scope) {
            $scope.FName = "Anil";
            $scope.LName = "Singh";
        });
    </script>
</head>

<body ng-app="angularApp">
    <div ng-controller="angularCtrl">
        First Name:    <input type="text" ng-model="FName"><br />
        Last Name:    <input type="text" ng-model="LName">
        <br />Full Name: {{FName + " " + LName}}
    </div>
</body>
</html>


The Output

About Blogger

ANIL SINGH

Hey! I am Anil Singh. I am Active Blogger, Writer and Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book