Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS:打开新窗口并维护双向数据绑定_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS:打开新窗口并维护双向数据绑定

Javascript AngularJS:打开新窗口并维护双向数据绑定,javascript,angularjs,Javascript,Angularjs,我希望打开一个新的浏览器窗口,其中包含角度$scope数据: <div ng-app="test" ng-controller="testController"> <input type="text" ng-model="var" /> <button ng-click="openWindow()">push!</button> </div> var test = angular.module('test', []); te

我希望打开一个新的浏览器窗口,其中包含角度
$scope
数据:

<div ng-app="test" ng-controller="testController">
<input type="text" ng-model="var" />
    <button ng-click="openWindow()">push!</button>
</div>

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

test.controller('testController', ['$compile', '$scope','$window', function($compile, $scope, $window) {
    $scope.var = 'hello'
    $scope.openWindow = function() {
        $scope.window = $window.open('', '_blank', 'width=500,height=400');
        angular.element($scope.window.document.body).append($compile($scope.var)($scope));
    };
}]);

推
var测试=角度模块('test',[]);
test.controller('testController',['$compile','$scope','$window',函数($compile,$scope,$window){
$scope.var='hello'
$scope.openWindow=函数(){
$scope.window=$window.open('''.'空白','宽度=500,高度=400');
元素($scope.window.document.body).append($compile($scope.var)($scope));
};
}]);

上述操作不会在窗口中返回任何数据。它像一个指令一样工作

如果我
append('hello world')
它可以工作。如果可能的话,有人能告诉我如何在不使用指令的情况下正确地绑定数据吗?

您可以通过绑定将数据附加到body编译元素,比如

$compile('<div>{{var}}</div>')($scope)

尝试在控制台中读取错误。您没有对新窗口的引用,FIDLE中的代码与中的代码不同question@charlietfl抱歉-fiddle已更新此。。。对于更高级的html,只需在编译后提取字符串所需的html即可it@charlietfl谢谢-我还需要它来维护双向数据绑定。我怀疑是范围问题,但我不知道如何使用postMessage API事件跨窗口更新。除非创建事件,否则新窗口中没有任何可用事件
$compile(angular.element($scope.window.document.body).html('{{var}}'))($scope);