What is $Scope in AngularJS? Tutorial with Example

What is $scope in AngularJS?

$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:

Setting up or adding Behavior

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.

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

  • Various member variables can be added to the scope object which can then be referenced in the view.
  • Behavior can be added to work with events which are generated for actions performed by the user.
  • The angularjs $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
top
X
Glad to have you at Our Website
Welcome to WPBot
wpChatIcon