Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. Scope is used as the "glue" that we use to communicate between the parent controller, the directive, and the directive template. Whenever the AngularJS application is bootstrapped, a rootScope object is created. Each scope created by controllers, directives and services are prototypically inherited from rootScope.
  2.  
  3. Yes, we can limit the scope on a directive . We can do so by creating an isolated scope for directive.
  4. There are 3 types of directive scopes:
  5. 1. Scope : False ( Directive uses its parent scope )
  6. 2. Scope : True ( Directive gets a new scope )
  7. 3. Scope : { } ( Directive gets a new isolated scope )
  8.  
  9. Directives with the new isolated scope: When we create a new isolated scope then it will not be inherited from the parent scope. This new scope is called Isolated scope because it is completely detached from its parent scope.
  10. Why? should we use isolated scope: We should use isolated scope when we want to create a custom directive because it will make sure that our directive is generic, and placed anywhere inside the application. Parent scope is not going to interfere with the directive scope.
  11.  
  12. Example of isolated scope:
  13.  
  14. var app = angular.module("test",[]);
  15.  
  16. app.controller("Ctrl1",function($scope){
  17. $scope.name = "Prateek";
  18. $scope.reverseName = function(){
  19. $scope.name = $scope.name.split('').reverse().join('');
  20. };
  21. });
  22. app.directive("myDirective", function(){
  23. return {
  24. restrict: "EA",
  25. scope: {},
  26. template: "<div>Your name is : {{name}}</div>"+
  27. "Change your name : <input type='text' ng-model='name'/>"
  28. };
  29. });
  30.  
  31. There’re 3 types of prefixes AngularJS provides for isolated scope these are :
  32. 1. "@" ( Text binding / one-way binding )
  33. 2. "=" ( Direct model binding / two-way binding )
  34. 3. "&" ( Behaviour binding / Method binding )
  35.  
  36. All these prefixes receives data from the attributes of the directive element like :
  37.  
  38. <div my-directive
  39. class="directive"
  40. name="{{name}}"
  41. reverse="reverseName()"
  42. color="color" >
  43. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement