AngularJS
What is AngularJS? Architecture & Features
What is AngularJS? AngularJS is an open source Model-View-Controller framework which is similar to...
$scope in AngularJS is a built-in object which basically binds the "controller" and the "view". One can define member variables in the scope within the controller which can then be accessed by the view.
Consider example below:
angular.module('app',[]).controller('HelloWorldCntrl'
function($scope)
{
$scope.message = "Hello World"
});Code Explanation:
In order to react to events or execute some sort of computation/processing in the View, we must provide behavior to the scope.
Behaviors are added to scope objects to respond to specific events that may be triggered by the View. Once the behavior is defined in the controller, it can be accessed by the view.
Let's look at an example of how we can achieve this.
<!DOCTYPE html>
<html lang="en">
<head>
<meta chrset="UTF 8">
<title>gtupapers</title>
</head>
<body ng-app="DemoApp">
<h1> gtupapers Global Event</h1>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<div ng-controller="DemoController">
{{fullName("Guru","99")}}
</div>
<script type="text/javascript">
var app = angular.module("DemoApp", []);
app.controller("DemoController", function($scope) {
$scope.fullName=function(firstName,lastname){
return firstName + lastname;
}
} );
</script>
</body>
</html>Code Explanation:
If the command is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
In the browser you will see a concatenation of both the values of Guru & 99 which were passed to the behavior in the controller.
Summary
$rootScope is the scope for the entire application. An application can only have one $rootScope and is used like a global variable. In Angular JS $scopes are child scopes and $rootScope is parent scope
What is AngularJS? AngularJS is an open source Model-View-Controller framework which is similar to...
By default, HTML does not provide the facility to include client-side code from other files. It's...
What is React JS? React is a Javascript library developed by Facebook which allows you to build UI...
We have prepared the most frequently asked Angular interview questions and answers that acquaint...
Sometimes the built-in filters in Angular cannot meet the needs or requirements for filtering...
What is Custom Directive? A custom directive in Angular Js is a user-defined directive with your...