Ng-Controller-2

 

In this example of Ng-Controller, We are taking two controller on two different html code blocks so the scope will be limited to their respective controllers. In the example we have controllers like: var app = angular.module('myApp', []); app.controller('myCtrl1', function ($scope) { $scope.firstName = "Learn"; }); app.controller('myCtrl2', function ($scope) { $scope.lastName = "kode"; }); And HTML part is like :

First Name:
Last Name:
 
 
 
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome to LearnKode - A code learning platform</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> </head> <body ng-app="myApp"> <div class="container"> <br /> <div ng-controller="myCtrl1"> First Name: <input class="form-control" type="text" ng-model="firstName"><br> </div> <div ng-controller="myCtrl2"> Last Name: <input class="form-control" type="text" ng-model="lastName"><br> </div> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl1', function ($scope) { $scope.firstName = "Learn"; }); app.controller('myCtrl2', function ($scope) { $scope.lastName = "Kode"; }); </script> </body> </html>
Output