【问题标题】:Angular JS: Can $scope be passed to an iframe and compile iframe's content using the passed scope?Angular JS:可以将 $scope 传递给 iframe 并使用传递的范围编译 iframe 的内容吗?
【发布时间】:2015-06-25 02:22:06
【问题描述】:

假设我有一个名为“test”的控制器:

$scope.testValue = "Hello World!";

index.html:

<div ng-controller="test">
    <iframe iframe-directive src="template.html"></iframe>
</div>

模板.html:

{{testValue}}

iframe 指令如何编译它并输出“Hello World”而不是“{{testValue}}”?

【问题讨论】:

  • 恐怕你不能在不同页面之间共享角度上下文,即你的index.htmltemplate.html
  • 你可以试试templateUrl in ngRoute: docs.angularjs.org/tutorial/step_07
  • 我一直在寻找答案,发现 $compile 可以做到。类似 $compile(elem.contents())(scope);但我就是不能让它工作。真的不可能吗?
  • 神圣的。 . .让它工作。我只是没有使用最新版本的 angularjs
  • 太棒了。你能分享一些 JSFiddle 吗?

标签: angularjs iframe compilation


【解决方案1】:

只要把它放在 iframe-directive 中就可以了

link: function(scope,elem,attrs) { $compile(elem.contents())(scope); }

【讨论】:

    猜你喜欢
    • 2015-10-17
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多