Scope in AngularJS

Scope is a special JavaScript object with the role of linking controllers and views. Scope contains information as model data. In the controller, model data can be accessed via the $ scope object.

Scope is a special JavaScript object with the role of linking controllers and views. Scope contains information as model data. In the controller, model data can be accessed via the $ scope object.

 

Here are the important points of the above example.

$ scope is passed as the controller's first parameter in its constructor.

$ scope.message and $ scope.type are models used in HTML pages.

We set the values ​​for the model and impact the Application Module with controller and nhanvienController.

We can define functions with $ scope.

Scope's inheritance in AngularJS

Scope is a separate controller. We define nested controllers so that the sub-controllers will inherit from the parent controllers.

 

Here are the main points through the above example.

We create the model variable value for nhanvienController.

We override the child controller's message as nhanvienITController. When "message" is used in nhanvienITController modules, the overwritten message value will be used.

For example

Below is an example for the above tutorial.

viduScope.html

 Vi du Scope in AngularJS 

 

 Cavalier AngularJS  ng-app = "ungdungAngularjs" ng-controller = "nhanvienController" > 
 {{message}} 
{{type}} ng-controller = "nhanvienITController" >
 {{message}} 
{{type}}

ng-controller="nhanvienBHController">

{{message}}
{{type}} src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">

Result:

Open viduScope.html page on web browser. And see the results below.

Scope in AngularJS Picture 1Scope in AngularJS Picture 1

Follow tutorialspoint

Previous article: View component in AngularJS

Next article: The Service in AngularJS

4 ★ | 2 Vote