What are scope and scope inheritance in AngularJS ? - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
What are scope and scope inheritance in AngularJS ?

What are scope and scope inheritance in AngularJS ?

 What are scope and scope inheritance in AngularJS ?

The $scope object used by views in AngularJS are organized into a hierarchy. There is a root scope, and the $rootScope can has one or more child scopes. Each controller has its own $scope (which is a child of the $rootScope), so whatever variables you create on $scope within controller, these variables are accessible by the view based on this controller.
For example - You have two controllers: ParentController and ChildController as given below:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Angular Scope Inheritance</title>
  5. <script src="lib/angular.js"></script>
  6. <script>
  7. var app = angular.module('ScopeChain', []);
  8.  
  9. app.controller("parentController", function ($scope) {
  10. $scope.managerName = 'Shailendra Chauhan';
  11. $scope.$parent.companyName = 'Dot Net Tricks'; //attached to $rootScope
  12. });
  13. app.controller("childController", function ($scope, $controller) {
  14. $scope.teamLeadName = 'Deepak Chauhan';
  15. });
  16. </script>
  17. </head>
  18. <body ng-app="ScopeChain">
  19. <div ng-controller="parentController ">
  20. <table style="border:2px solid #e37112">
  21. <caption>Parent Controller</caption>
  22. <tr>
  23. <td>Manager Name</td>
  24. <td></td>
  25. </tr>
  26. <tr>
  27. <td>Company Name</td>
  28. <td></td>
  29. </tr>
  30. <tr>
  31. <td>
  32. <table ng-controller="childController" style="border:2px solid #428bca">
  33. <caption>Child Controller</caption>
  34. <tr>
  35. <td>Team Lead Name</td>
  36. <td></td>
  37. </tr>
  38. <tr>
  39. <td>Reporting To</td>
  40. <td></td>
  41. </tr>
  42. <tr>
  43. <td>Company Name</td>
  44. <td></td>
  45. </tr>
  46. </table>
  47. </td>
  48. </tr>
  49. </table>
  50. </div>
  51. </body>
  52. </html>

Angular Scope Inheritance

The contenders:
  • The following create new scopes, and inherit prototypically: ng-repeat, ng-include, ng-switch, ng-view, ng-controller, directive with scope: true, directive with transclude: true.
  • The following creates a new scope which does not inherit prototypically: directive with scope: { ... }. This creates an "isolate" scope instead.
Note, by default, directives do not create new scope -- i.e., the default is scope: false.
ng-include
Suppose we have in our controller:
$scope.myPrimitive = 50;
$scope.myObject    = {aNumber: 11};
And in our HTML:
<script type="text/ng-template" id="/tpl1.html">
    <input ng-model="myPrimitive">
</script>
<div ng-include src="'/tpl1.html'"></div>

<script type="text/ng-template" id="/tpl2.html">
    <input ng-model="myObject.aNumber">
</script>
<div ng-include src="'/tpl2.html'"></div>

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.