AngularJS Scopes
The Scope is an object that is specified as a binding part between the HTML (view) and the JavaScript (controller). It plays a role of joining controller with the views. It is available for both the view and the controller.
How to use Scope
To make a controller in AngularJS, you have to pass the $scope object as an argument.
See this example:
1) Sample Scope data.
<html>
<head>
<title>scope-data-model-production</title>
<script src="angular.min.js"></script>
<script>
(function(angular) {
'use strict';
angular.module('scopeExample', [])
.controller('MyController', ['$scope', function($scope) {
$scope.username = 'World';
$scope.sayHello = function() {
$scope.greeting = 'Hello ' + $scope.username + '!';
};
}]);
})(window.angular);
</script>
</head>
<body ng-app="scopeExample">
<div ng-controller="MyController">
Your name:
<input type="text" ng-model="username">
<button ng-click='sayHello()'>greet</button>
<hr>
{{greeting}}
</div>
</body>
</html>
Scope Inheritance :
Scope is controller-specific. If we define nested controllers, then the child controller inherits the scope of its parent controller.
<html>
<head>
<title>Angular JS Forms</title>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app="mainApp" ng-controller="shapeController">
<p>{{message}} <br/> {{type}} </p>
<div ng-controller="circleController">
<p>{{message}} <br/> {{type}} </p>
</div>
<div ng-controller="squareController">
<p>{{message}} <br/> {{type}} </p>
</div>
</div>
<script src="angular.min.js"></script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller("shapeController", function($scope) {
$scope.message = "In the Progamming Subject Is:";
$scope.type = "Java";
});
mainApp.controller("circleController", function($scope) {
$scope.message = "In the Language Subject Is:";
});
mainApp.controller("squareController", function($scope) {
$scope.message = "In the WebDesigning Subject Is:";
$scope.type = "Html,Photoshop";
});
</script>
</body>
</html>
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743