2015-12-11 2 views
0

Я использую AngularJS v1.4.1, и у меня есть следующая директива. Я думал, что работаю, но теперь по какой-то причине, когда моя страница загружает директиву, вызывается дважды. Я проверил все, что мог, но я не вижу ничего другого, кроме как раньше, кроме того, что директива больше не работает. В частности, что происходит, кажется, что его дважды вызывают.

app.directive('pagedownAdmin', ['$compile','$timeout', function ($compile, $timeout) { 
    var nextId = 0; 
    var converter = Markdown.getSanitizingConverter(); 
    converter.hooks.chain("preBlockGamut", function (text, rbg) { 
     return text.replace(/^ {0,3}""" *\n((?:.*?\n)+?) {0,3}""" *$/gm, function (whole, inner) { 
      return "<blockquote>" + rbg(inner) + "</blockquote>\n"; 
     }); 
    }); 

    return { 
     require: 'ngModel', 
     replace: true, 
     scope: { 
      modal: '=modal' 
     }, 
     template: '<div class="pagedown-bootstrap-editor"></div>', 
     link: function (scope, iElement, attrs: any, ngModel) { 

      var editorUniqueId; 

      if (attrs.id == null) { 
       editorUniqueId = nextId++; 
      } else { 
       editorUniqueId = attrs.id; 
      } 

      scope.showPagedownButtons = function() { 
       document.getElementById("wmd-button-bar-" + editorUniqueId).style.display = 'block'; 
      }; 

      var newElement = $compile(
       '<div>' + 
       '<div class="wmd-panel">' + 


       '<div data-ng-hide="modal.wmdPreview == true" id="wmd-button-bar-' + editorUniqueId + '" style="display:none;"></div>' + 

       '<textarea ng-click="showPagedownButtons()" data-ng-hide="modal.wmdPreview == true" class="wmd-input" id="wmd-input-' + editorUniqueId + '">' + 
       '</textarea>' + 

       '</div>' + 

       '<div data-ng-show="modal.wmdPreview == true" id="wmd-preview-' + editorUniqueId + '" class="pagedownPreview wmd-panel wmd-preview"></div>' + 

       '</div>')(scope); 

      // iElement.html(newElement); 
      iElement.append(newElement); 

      var hide = function() { 
       document.getElementById("wmd-button-bar-" + editorUniqueId).style.display = 'none'; 
      } 

      var editor = new Markdown.Editor(converter, "-" + editorUniqueId, { 
       handler: hide 
      }); 

      // var $wmdInput = iElement.find('#wmd-input-' + editorUniqueId); 
      var $wmdInput = angular.element(document.getElementById("wmd-input-" + editorUniqueId)); 



      var init = false; 

      editor.hooks.chain("onPreviewRefresh", function() { 
       var val = $wmdInput.val(); 
       if (init && val !== ngModel.$modelValue) { 
        $timeout(function() { 
         scope.$apply(function() { 
          ngModel.$setViewValue(val); 
          ngModel.$render(); 
         }); 
        }); 
       } 
      }); 

      ngModel.$formatters.push(function (value) { 
       init = true; 
       $wmdInput.val(value); 
       editor.refreshPreview(); 
       return value; 
      }); 

      editor.run(); 
     } 
    } 
}]); 

Вот код, который вызывает директиву:

<textarea data-pagedown-admin 
          data-modal="cos" 
          id="contentText" 
          name="contentText" 
          ng-minlength="5" 
          ng-model="cos.content.text" 
          ng-required="true"></textarea> 

Когда я отлаживать директиву, поставив точку останова на «вар editorUniqueId», то я вижу, это идет туда дважды.

Есть ли у кого-нибудь идеи, что может произойти?

ответ

0

Я делал это в прошлом без проблем:

var newElement = '<div>' + 
    // ... more HTML ... 
'</div>'; 

iElement.append($compile(newElement)(scope)); 
+0

Я рад, что это сработало –

Смежные вопросы