Guest User

Untitled

a guest
Dec 11th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. /**
  2. * Module definition and dependencies
  3. */
  4. angular.module('App.Parent', [])
  5.  
  6. /**
  7. * Component
  8. */
  9. .component('parent', {
  10. templateUrl: 'parent.html',
  11. controller: 'ParentCtrl',
  12. })
  13.  
  14. /**
  15. * Controller
  16. */
  17. .controller('ParentCtrl', function($parentDep) {
  18.  
  19. //Get controller
  20. const $ctrl = this;
  21.  
  22. /**
  23. * On init
  24. */
  25. this.$onInit = function() {
  26.  
  27. //Do stuff
  28. this.something = true;
  29. };
  30. });
  31.  
  32. /**
  33. * Module definition and dependencies
  34. */
  35. angular.module('App.Child', [])
  36.  
  37. /**
  38. * Component
  39. */
  40. .component('child', {
  41. templateUrl: 'child.html',
  42. controller: 'ChildCtrl',
  43. })
  44.  
  45. /**
  46. * Controller
  47. */
  48. .controller('ChildCtrl', function($controller, $parentDep) {
  49.  
  50. //Get controllers
  51. const $ctrl = this;
  52. const $base = $controller('ParentCtrl', {$parentDep});
  53.  
  54. //Extend
  55. angular.extend($ctrl, $base);
  56.  
  57. /**
  58. * On init
  59. */
  60. this.$onInit = function() {
  61.  
  62. //Call parent init
  63. $base.$onInit.call(this);
  64.  
  65. //Do other stuff
  66. this.somethingElse = true;
  67. };
  68. });
  69.  
  70. angular
  71. .module('app')
  72. .factory('component.utils', function() {
  73. return {
  74. sharedProp: 'foo',
  75. sharedMethod: function() { return 'bar' }
  76. }
  77. })
  78. // components can all easily use functionality
  79. // provided by one (or more) services, w/o
  80. // needing a complicated inheritance model.
  81. .component('foo', {
  82. templateUrl: 'foo.html',
  83. controller: [
  84. 'component.utils',
  85. function(utils) {
  86. this.$onInit = function() {
  87. this.prop = utils.sharedProp;
  88. utils.sharedMethod();
  89. // now do other foo things...
  90. }
  91. }
  92. ]
  93. })
  94. .component('bar', {
  95. templateUrl: 'foo.html',
  96. controller: [
  97. 'component.utils',
  98. function(utils) {
  99. this.$onInit = function() {
  100. this.prop = utils.sharedProp;
  101. utils.sharedMethod();
  102. // now do other bar things...
  103. }
  104. }
  105. ]
  106. });
Add Comment
Please, Sign In to add comment