【问题标题】:How to load Gist code without getting async error如何加载 Gist 代码而不会出现异步错误
【发布时间】:2015-02-27 15:51:24
【问题描述】:

我目前正在为自己的一个新博客使用 angular 和 rails,但我被困住了。我使用ng-bind-html 从后端注入代码。

现在我使用自己的指令修复了它。

当我尝试从 Github 的 Gist 服务导入代码示例时,我目前收到以下错误:

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

这是什么原因以及如何解决这个问题?提前致谢!

【问题讨论】:

  • 看起来很明显,你不能在页面加载后使用document.write,否则它会清除页面中的所有内容
  • 需要更多信息/代码示例/用于帮助您的库。我遇到了类似的问题。我使用了gist-embed 库。使用 ng-bind-html 时,我必须使用 gist() 重新编译绑定的 html。也看看github.com/pasupulaphani/angular-gist-embed,它可能对你有帮助。

标签: javascript ruby-on-rails angularjs asynchronous


【解决方案1】:

这个问题已经很老了,但我在搜索错误时偶然发现了它。您可以在项目中包含https://github.com/blairvanderhoof/gist-embed/,然后编写指令来应用更改:

yourApp.directive('gistDirective', function() {
    return function(scope, element, attrs) {
        scope.$watch('someAngularVariable', function(){
            angular.element('[data-gist-id]').gist();
        });

    }
});

然后

<section gist-directive>
                <article>
                    <div ng-bind-html="trustHtml(someAngularVariable.body)"></div>
                </article>
    </section>

【讨论】:

    猜你喜欢
    • 2014-03-11
    • 2017-05-02
    • 2017-10-09
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多