绑定角度交叉的iframe,可能吗?

End*_*ess 9 angularjs

有没有什么方法可以绑定/共享相同的范围/控制器/指令和东西到同一域中的另一个iframe?

如果我可以在另一个iframe中拥有相同的ngApp,那将会很酷.以前有人做过这样的事吗?

iCo*_*nor 11

是的,这是可能的,你不需要做任何特别的事情,你只需要编译内容.

这是一个例子:http://jsfiddle.net/gWgYM/

HTML:

<div ng-app="app" ng-controller="test">
    <h1>The date is {{date}}</h1>
    <iframe frame=""></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

使用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);
    };
});
Run Code Online (Sandbox Code Playgroud)