gpt4 book ai didi

angularjs - 绑定(bind) Angular 交叉 iframe,可能吗?

转载 作者:行者123 更新时间:2023-12-02 22:12:28 25 4
gpt4 key购买 nike

有什么方法可以绑定(bind)/共享相同的作用域/ Controller /指令并将其填充到同一域中的另一个 iframe 中吗?

如果我也能在另一个 iframe 中拥有相同的 ngApp,那就太酷了。以前有人做过类似的事情吗?

最佳答案

是的,这是可能的,您不需要做任何特殊的事情,您只需要编译内容即可。

这是一个示例: 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);
};
});

关于angularjs - 绑定(bind) Angular 交叉 iframe,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13881834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com