All for Joomla All for Webmasters

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.  <!DOCTYPE html>
  2. <html>
  3. <script src=“”></script>
  4. <body>
  5. <div ng-app=“myApp” ng-controller=“myCtrl”>
  6. <h1>{{carname}}</h1>
  7. </div>
  8. <script>
  9. var app = angular.module(‘myApp’, []);
  10. app.controller(‘myCtrl’, function($scope) {
  11.     $scope.carname = “Volvo”;
  12. });
  13. </script>
  14. <p>The property “carname” was made in the controller, and can be referred to in the view by using the {{ }} brackets.</p>
  15. </body>
  16. </html>
submit to reddit

Leave a Reply