Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. app.directive('html', ['$timeout','$window', function ($timeout, $window) {
  4.     return {
  5.         restrict: 'A',
  6.         link: function (scope, element, attrs) {
  7.             function parseHtml(tries) {
  8.                 // a sanity check, just in case we reuse this function as a handler,
  9.                 // e.g. for `orientationchange`
  10.                 if (isNaN(+tries)) {
  11.                   tries = attrs.maxTries || 10;
  12.                 }
  13.  
  14.                 if (tries > 0) {
  15.                   $timeout(function() {
  16.                     var width = parseInt($window.getComputedStyle(element[0]).width, 10);
  17.                     if (width) {
  18.                       // if width is computed, the element is ready to be manipulated
  19.  
  20.                       //parse html and scripts from snippet
  21.                         element.html(attrs.html);
  22.                       // ...
  23.                     } else {
  24.                       // otherwise, the element is not ready, so decrement the tries,
  25.                       // wait a bit, and try again:
  26.                       parseHtml(tries - 1);
  27.                     }
  28.                   }, attrs.msDelay || 100);
  29.                 } else {
  30.                   // if we got here, we've exhausted our tries, so we probably
  31.                   // want to log or warn or throw here.
  32.                 }
  33.             }
  34.  
  35.             parseHtml(attrs.maxTries);
  36.         }
  37.     };
  38. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement