【问题标题】:AngularJS: Bind scope to REST retrieved iframeAngularJS:将范围绑定到 REST 检索的 iframe
【发布时间】:2015-08-29 09:01:45
【问题描述】:

我有一个表格来修改我的范围。然后使用此范围更新我网站中的另一部分。 那部分,我现在想放入一个单独的(模板)文件并动态加载到 iframe 中。

如果我向 Iframe 提供静态 HTML,它就可以工作,但我无法通过加载另一个文件作为模板来运行它。

这是我当前的代码:

cvFormApp.directive('cvframe', function($compile, $http, appConfig) {
  $http({
    url: appConfig.baseUrl + 'res/templates/classic.html',
    method: 'GET'
  }).then(function(response) {
    tpl = response.data;
  });

  return function($scope, $element, tpl) {

    var $body = angular.element($element[0].contentDocument.body),
      template = $compile('<h1>The date in the iframe is {{date}}</h1>')($scope);
    $body.append(template);
  };
});

如何将引用的 classic.html 插入 iframe 并绑定到我的其余代码?

【问题讨论】:

    标签: angularjs iframe asynchronous angularjs-directive angular-http


    【解决方案1】:

    我不确定你想要什么。我认为您在使用 $http 调用异步加载模板时弄乱了异步响应。您应该从 $http 成功中添加该模板。

    指令

    cvFormApp.directive('cvframe', function($compile, $http, appConfig) {
        return function($scope, $element, tpl) {
          $http({
            url: appConfig.baseUrl + 'res/templates/classic.html',
            method: 'GET'
          }).then(function(response) {
            var $body = angular.element($element[0].contentDocument.body), 
                        template = response.data;
            $body.append($compile(template)(scope)); //appended compiled element to DOM
          });
        };
    });
    

    【讨论】:

    • 太好了,这行得通。但不知何故,它没有编译加载的内容。它留下了很多 {{variables}} 而不是将其绑定到范围。
    • 你需要用当前作用域编译模板变量..会让它工作..
    • 成功了!谢谢。
    • @nogga 很高兴为您提供帮助..谢谢 :)
    猜你喜欢
    • 2013-03-17
    • 1970-01-01
    • 2014-12-08
    • 2018-01-28
    • 2013-09-09
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多