Angular - добавить html через разные кадры с одним модулем на кадр

Мой код имеет основной windonw и один iframe и каждый со своим модулем. Кнопка в главном окне вызывает событие click, которое должно добавить html в iframe, новый html при добавлении в него должен правильно применять перехватчики и директивы, но это не работает! Угловой JavaScript:

angular.module('module1',[]).controller('Controller1', function ($scope) {
  $scope.get = function(){
    $http.jsonp("some_url_here").success(function(html){
      $scope.content = html;
    });
  }
}).directive('click', function($compile) {
  return {
    link: function link(scope, element, attrs) {
      element.bind('click',function(){
        var unbind = scope.$watch(scope.content, function() {
        var div=document.getElementById("frame").contentWindow.angular.element("divId");
        div.append($compile(scope.content)(div.scope()));

          unbind();
        });
      });
    }
  }
});


angular.module('module2',[]).directive('a', function() {
  return {
    restrict:'E',
    link: function(scope, element, attrs) {
      console.log('ping!');
      console.log(attrs.href);
    }
 };
});

HTML-код:

<html ng-app="modile1">
  <div ng-controller="Controller1">
    <button type="button", ng-click="get('any_value')", click:""/> Load frame
  </div>

  <iframe id="frame" src="/please/ignore/this">
   <!-- considere the html as appended from iframe-src and contains ng-app="module2" -->
   <html ng-app="module2">
     <div id="divId">
      <!-- code should be inject here -->
     </div>
   </html>
  </iframe>
</html>

Пожалуйста, учтите, что angularjs, jquery, если применимо, объявление модулей, а также заголовки загружены правильно.

Я хотел бы загрузить html-контент из основного фрейма / окна в iframe и правильно запустить перехватчики и директивы. Является ли это возможным? Если да, как я могу это сделать?

Спасибо за продвижение!

1 ответ

Решение

Я пробовал этот код, и, кажется, работает отлично! Я нашел это здесь: http://www.snip2code.com/Snippet/50430/Angular-Bootstrap

var $rootElement = angular.element(document.getElementById("frame").contentWindow.document);
var modules = [
  'ng',
  'module2',
  function($provide) {
    $provide.value('$rootElement', $rootElement)
  }
];

var $injector = angular.injector(modules);

var $compile = $injector.get('$compile');

$rootElement.find("div#divId").append(scope.content);

var compositeLinkFn = $compile($rootElement);

var $rootScope = $injector.get('$rootScope');
compositeLinkFn($rootScope);

$rootScope.$apply();
Другие вопросы по тегам