Guest User

Untitled

a guest
Dec 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. <div ng-include="'item.html'"
  2. ng-controller="ItemController"
  3. onload="item = rightItem">
  4. </div>
  5.  
  6. $scope.dataHolder = {};
  7.  
  8. $scope.dataHolder.leftItem = items.leftItem;
  9. $scope.dataHolder.rightItem = items.rightItem;
  10.  
  11. <div ng-include="'item.html'"
  12. ng-if="true"
  13. onload="item = rightItem">
  14. </div>
  15. <div ng-include="'item.html'"
  16. ng-if="true"
  17. onload="item = leftItem">
  18. </div>
  19.  
  20. .directive('ngIncludeTemplate', function() {
  21. return {
  22. templateUrl: function(elem, attrs) { return attrs.ngIncludeTemplate; },
  23. restrict: 'A',
  24. scope: {
  25. 'ngIncludeVariables': '&'
  26. },
  27. link: function(scope, elem, attrs) {
  28. var vars = scope.ngIncludeVariables();
  29. Object.keys(vars).forEach(function(key) {
  30. scope[key] = vars[key];
  31. });
  32. }
  33. }
  34. })
  35.  
  36. <div ng-include-template="'item.html'" ng-include-variables="{ item: 'whatever' }"></div>
  37. <div ng-include-template="'item.html'" ng-include-variables="{ item: variableWorksToo }"></div>
  38.  
  39. .directive(
  40. 'ngIncludeTemplate'
  41. () ->
  42. {
  43. templateUrl: (elem, attrs) -> attrs.ngIncludeTemplate
  44. restrict: 'A'
  45. scope: {
  46. 'ngIncludeVariables': '&'
  47. }
  48. link: (scope, elem, attrs) ->
  49. vars = scope.ngIncludeVariables()
  50. for key, value of vars
  51. scope[key] = value
  52. }
  53. )
  54.  
  55. <div ng-include='myFile.html' ng-init="myObject = myCtrl.myObject; myOtherObject=myCtrl.myOtherObject"/>
  56.  
  57. .directive('ngIncludeTemplate', function() {
  58. return {
  59. templateUrl: function(elem, attrs) { return attrs.ngIncludeTemplate; },
  60. restrict: 'A',
  61. scope: {
  62. 'ngIncludeVariables': '&'
  63. },
  64. link: function(scope, elem, attrs) {
  65. var cache = scope.ngIncludeVariables();
  66. Object.keys(cache).forEach(function(key) {
  67. scope[key] = cache[key];
  68. });
  69.  
  70. scope.$watch(
  71. function() {
  72. var val = scope.ngIncludeVariables();
  73. if (angular.equals(val, cache)) {
  74. return cache;
  75. }
  76. cache = val;
  77. return val;
  78. },
  79. function(newValue, oldValue) {
  80. if (!angular.equals(newValue, oldValue)) {
  81. Object.keys(newValue).forEach(function(key) {
  82. scope[key] = newValue[key];
  83. });
  84. }
  85. }
  86. );
  87.  
  88. }
  89. };
  90. });
  91.  
  92. <script type="text/ng-template" id="partial">{{variable}}</script>
  93.  
  94. <div ng-include-template="'partial'" ng-include-variables="{variable: variable}"></div>
  95.  
  96. templateUrl: function(elem, attrs) { return attrs.ngIncludeTemplate; },
  97.  
  98. template: function(elem, attrs) { return document.getElementById(attrs.ngIncludeTemplate.split("'")[1]).innerHTML },
Add Comment
Please, Sign In to add comment