在 AngularJS 指令中附加 Pagedown 时出错 [英] Error appending Pagedown in AngularJS Directive

查看:25
本文介绍了在 AngularJS 指令中附加 Pagedown 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于这个问题,虽然我觉得这值得它自己的问题:Google pagedown AngularJS 指令

Based on this question, though I felt this warranted its own question: Google pagedown AngularJS directive

按照此问题中的示例,它似乎有效,但是当我尝试将指令附加到页面时遇到了问题.

Following the example from this question, it seems to work, however I am running into issues when I try to append a directive to the page.

这是我在链接函数中的代码:

Here is the code I have in the linking function:

  scope.editor_id = null;

  if(attrs.id == null) {
    scope.editor_id = nextID++;
  } else {
    scope.editor_id = attrs.id;
  }

  //append editor HTML
  //has to be done here so that it is available to the editor when it is run below
  var editor_html = $compile(
      '<div id="wmd-button-bar-' + scope.editor_id + '"></div>' +
      '<textarea class="wmd-input" id="wmd-input-' + scope.editor_id + '" ng-model="content"></textarea>'
  )(scope);


  element.find('.wmd-panel').append(editor_html);


  var editor = new Markdown.Editor(editor_converter, "-" + scope.editor_id);
  editor.run();

但是,当我将其中一个附加到文档时,出现以下错误:

However, when I append one of these to the document, I get the following error:

TypeError: Cannot read property 'attachEvent' of null

wmd-input 不在 HTML 中时,往往会出现此错误.但是,我使用 $compile 函数添加它,它在页面加载时起作用,但在附加时不起作用.我在这里做错了什么?

This error tends to crop up when the wmd-input is not present in the HTML. however, I am adding it with the $compile function, and it works on page load, but not when it is appended. What am I doing wrong here?

推荐答案

UPDATE

我能够重现您的问题:http://plnkr.co/edit/R2eDKBreHmYBjPtU0JxD?p=预览

  • 我之前的假设是错误的(复合链接函数确实返回元素)
  • 问题在于您使用 angular.element#find 的方式.
  • angular.element#find 只搜索不在整个文档中的子元素.
  • 具有 .wmd-panel 类的 DOM 元素不是当前元素的子元素.
  • I was wrong with my previous assumption ( the composite linking function do returns the element)
  • The problem is with the way you use angular.element#find.
  • angular.element#find only search for child elements not on the whole document.
  • the DOM element with a .wmd-panel class is not a child of the current element.
angular.element('.wmd-panel').append(editor_html);

这篇关于在 AngularJS 指令中附加 Pagedown 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆