Advertisement
Guest User

Untitled

a guest
Aug 12th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. <ng-view>
  2. <viewer doc="getDocument()">
  3. </viewer>
  4. </ng-view>
  5.  
  6.  
  7. angular.directive('viewer', ['$compile', '$anchorScroll', function($compile, $anchorScroll) {
  8. return function(scope, element, attrs) {
  9. scope.$watch(
  10. function(scope) {
  11. var doc = scope.$eval(attrs.doc);
  12. if (!doc)
  13. return ""
  14.  
  15. return doc.html;
  16. },
  17. function(value) {
  18. element.html(value);
  19. $compile(element.contents())(scope);
  20. }
  21. );
  22. };
  23. }]);
  24.  
  25. angular.directive('i18n', ['$rootScope', 'LocaleService', function($rootScope, LocaleService) {
  26. var cleanup;
  27. return {
  28. restrict: 'EAC',
  29. compile: function(element, attrs) {
  30. var originalText = element.text();
  31. element.text(LocaleService.getTranslation(originalText, attrs.locale));
  32. cleanup = $rootScope.$on('locale-changed', function(locale) {
  33. element.text(LocaleService.getTranslation(originalText, attrs.locale || locale));
  34. });
  35. },
  36. link: function(scope) {
  37. scope.$on('$destroy', function() {
  38. console.log("destroy");
  39. cleanup();
  40. });
  41. }
  42. };
  43. }]);
  44.  
  45. angular.directive('i18n', ['$rootScope', 'LocaleService', function($rootScope, LocaleService) {
  46. return {
  47. restrict: 'EAC',
  48. link: function(scope, element, attrs) {
  49. var cleanup;
  50. var originalText = element.text();
  51. element.text(LocaleService.getTranslation(originalText, attrs.locale));
  52. cleanup = $rootScope.$on('locale-changed', function(locale) {
  53. element.text(LocaleService.getTranslation(originalText, attrs.locale || locale));
  54. });
  55. scope.$on('$destroy', function() {
  56. console.log("destroy");
  57. cleanup();
  58. });
  59. }
  60. };
  61. }]);
  62.  
  63. angular.directive('i18n', ['$rootScope', 'LocaleService', function($rootScope, LocaleService) {
  64. return {
  65. restrict: 'EAC',
  66. link: function(scope, element, attrs) {
  67. var originalText = element.text();
  68. setElText();
  69. function setElText(locale){
  70. element.text(LocaleService.getTranslation(originalText, attrs.locale || locale));
  71. }
  72. scope.$on('locale-changed', setElText);
  73. }
  74. };
  75. }]);
  76.  
  77. $rootScope.$broadcast('locale-change', 'en-AU');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement