2012-12-14 12 views
9

Czy istnieje sposób na powiązanie/udostępnienie tego samego zakresu/kontrolerów/dyrektyw i innych elementów w innym elemencie iframe w tej samej domenie?Powiązać kątowe elementy iframe, możliwe?

Byłoby fajnie, gdybym mógł mieć ten sam ngApp w innym iframe również. Czy ktoś wcześniej zrobił coś takiego?

Odpowiedz

11

Tak, jest to możliwe i nie trzeba robić nic specjalnego, wystarczy skompilować zawartość.

Oto przykład: http://jsfiddle.net/gWgYM/

HTML:

<div ng-app="app" ng-controller="test"> 
    <h1>The date is {{date}}</h1> 
    <iframe frame=""></iframe> 
</div> 

Javascript:

var app = angular.module('app', []); 

app.controller('test', function($scope) { 
    $scope.date = new Date(); 
    window.setInterval(function() { 
     $scope.date = new Date(); 
     $scope.$apply(); 
    }, 1000); 
}); 

app.directive('frame', function($compile) { 
    return function($scope, $element) { 
     var $body = angular.element($element[0].contentDocument.body), 
      template = $compile('<p>The date in the iframe is {{date}}</p>')($scope); 
     $body.append(template); 
    }; 
}); 
Powiązane problemy