简体   繁体   English

从AngularJs的外部站点在iframe中加载特定的div

[英]Load specific div in iframe from external site in AngularJs

Problem Question - 问题问题-

How to load a specific div from the external site into the iframe? 如何将特定div从外部网站加载到iframe? I am aware of the Same Origin Policy and external Site belongs to me. 我知道同源政策和外部网站属于我。 How would I do this in AngularJs. 我将如何在AngularJs中做到这一点。

I looked for this but most of the answers are in jQuery only. 我寻找了这个,但大多数答案仅在jQuery中。

Thanks for the help 谢谢您的帮助

Update 1 更新1

Let me be more specific. 让我更具体一点。 For instance if you use iframe, it gives you access to everything. 例如,如果您使用iframe,则可以访问所有内容。 But I only want a Particular DIV of of this external URL. 但我只想要此外部URL的特定DIV。

<iframe style="border: 0" width="800" height="600" frameborder="0" scrolling="no" src="http://localhost:8888/">

Typically, blindly and thoughtlessly using jQuery will not play nice with Angular. 通常,盲目而漫不经心地使用jQuery在Angular中无法很好地发挥作用。 This is not so much because of jQuery - the library, but more so because Angular and jQuery promote different programming paradigms. 这并不是因为使用jQuery(库),而是因为Angular和jQuery促进了不同的编程范例。

Nevertheless, jQuery is a toolbox, and one such tool is jQuery.load() . 尽管如此,jQuery是一个工具箱,而jQuery.load()就是这样一种工具。

The best way, in my mind, to make it work with Angular is to create a directive that uses jQuery.load() : 在我看来,使其与Angular一起使用的最佳方法是创建一个使用jQuery.load()的指令:

.directive("load", function($compile){
  return {
    restrict: "A",
    scope: true,
    link: function(scope, elem, attrs){

      attrs.$observe('load', function(newValue){
        elem.load(newValue, null, function(){

          // compile the newly imported div
          $compile(elem.contents())(scope);

          // need to force $digest, since this is outside of Angular digest cycle
          scope.$digest();
        });
      });
    }
  };
});

Then you could use it like so: 然后,您可以像这样使用它:

<div load="foo.html #id"></div>

or, 要么,

<div load="{{urlWithId}}"></div>

Be sure to load jQuery before Angular, otherwise .load will not be defined. 确保在Angular之前加载jQuery,否则将不会定义.load

Plunker Plunker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM