1. AngularJS Scope (scope)
- Scope (scope) is the link between HTML (view) and JavaScript (controller).
- Scope is an object that has methods and properties Available.
- Scope can be applied to views and Controllers.
2. Use Scope: when you create a controller in AngularJS, you can pass $Scope object as a parameter
<!DOCTYPE HTML><HTML> <Head> <MetaCharSet= "UTF-8"> <title></title> <Scripttype= "text/javascript"src= "js/angular.min.js"></Script> <style>Input.ng-invalid{Background-color:LightBlue; } </style> </Head> <Body> <DivNg-app= "myApp"Ng-controller= "myctrl"> <H1>{{carname}}</H1> </Div> <Script> varapp=Angular.module ("myApp", []); App.controller ('Myctrl', function($scope) {$scope. carname= 'Volvo'; }) </Script> <P>The controller creates a property name of "carname" that corresponds to the name in the view that is used in the {{}}.</P> </Body></HTML>
3. Scope Overview: Scope is a model. Scope is a JavaScript object with properties and methods that can be used in views and controllers.
The AngularJS application consists of the Following:
- view, which is HTML.
- Model (models), the data available in the current VIEW.
- controller, the JavaScript function, can add or modify Properties.
4. Scope Scope: In a large project, there are multiple scopes in the HTML DOM, so you need to know which scope corresponds to the scope you are using. In the example above we can find that the first value when creating a controller is the value of the property that we ng-controller in an element, and if there are no matching elements, then the controller created has no effect and an exception occurs. When the match succeeds, the properties and methods that we set in the JS code can be used in the matching element, when the scope of the $scope is in the start and end tags of the matching element.
5. Root Scope: $rootScope, which can function in all HTML elements contained in the Ng-app Directive. is the bridge of scope in each controller. Values defined with Rootscope can be used in each controller.
<!DOCTYPE HTML><HTML> <Head> <MetaCharSet= "UTF-8"> <title></title> <Scripttype= "text/javascript"src= "js/angular.min.js"></Script> <style>Input.ng-invalid{Background-color:LightBlue; } </style> </Head> <Body> <DivNg-app= "myApp"> <DivNg-controller= "myCtrl1"> <H1>{{carname}}</H1> </Div> <P>{{name}}</P> </Div> <Script> varapp=Angular.module ("myApp", []); App.controller ('MYCTRL1', function($scope, $rootScope) {$scope. carname= 'Volvo'; $rootScope. name= 'WGL' }) </Script> <P>A property name "carname" is created in the controller and can only be used in the Controller.</P> <P>The controller creates a property name "name" with the scope, which can be used in all HTML elements contained in the Ng-app Directive.</P> </Body></HTML>
AngularJS Scope (scope)