从不同框架内的服务访问AngularJS服务

hie*_*ppe 10 javascript iframe angularjs

在AngularJS应用程序(主要)中,我有一个iframe内部,还有另一个AngularJS应用程序(iframe)也在我的控制之下.我想在两个服务之间共享数据,一个在主应用程序中,另一个在iframe应用程序中.它们都需要读取和写入同一个对象.

// main
// ... routes ...
views: { main: {
  controller: function ($scope, serviceA) {
    $scope.serviceA = serviceA;
  },
  templateUrl: 'iframe.html'
}
// ...
function ServiceA () {
  this.sharedData; // exposed to controllers in main app
}
// ...

// iframe
// ...
function ServiceB () {
  this.sharedData; // exposed to controllers in iframe app
}
// ...
Run Code Online (Sandbox Code Playgroud)

当在iframe应用程序中的控制器内部时,我设法引用serviceA.sharedData如下:

var self = this;
var parentScope = $window.parent.angular.element($window.frameElement).scope();
parentScope.$watch('serviceA.sharedData', function (newValue, oldValue) {
  self.sharedData = newValue;
}
Run Code Online (Sandbox Code Playgroud)

这可以实现吗?如何实现?

我已阅读以下内容,但无法将其转化为解决方案,但是:

Deu*_*rco 7

我设法做了一些有用的事情,对你有用.这不是完美的,但是一个好的开始.这是代码:

父页面:

<div ng-controller="ParentController">
    <h1>Hello, parent page!</h1>

    <p><strong>Parent model:</strong></p>
    <p>
        <input type="text"
           ng-model="data.foo"
           placeholder="Enter the thing you want to share"/>
    </p>

    <p><strong>Parent result:</strong></p>
    <p>{{ data.foo }}</p>

    <iframe src="child-page.html" frameborder="0"></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

子页面:

<div ng-controller="ChildController">
    <h1>Hello, child page!</h1>

    <p><strong>Child result:</strong></p>
    <p>{{ data.foo }}</p>
</div>
Run Code Online (Sandbox Code Playgroud)

app.js

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

app.factory('DataService', ['$rootScope', '$window', function ($rootScope, $window) {
    var // Variables
        dataScope,

        // Functions
        getScope;

    dataScope = $rootScope.$new(true);

    getScope = function () {
        return dataScope;
    };

    $window.DataService = {
        getScope: getScope
    };

    return {
        getScope: getScope
    };
}]);

app.controller('ParentController', ['$scope', 'DataService', function ($scope, DataService) {
    $scope.data = DataService.getScope();
}]);

app.controller('ChildController', ['$scope', '$window', '$interval', function (
    $scope,
    $window,
    $interval
) {
    $scope.data = $window.parent.DataService.getScope();

    // makes a $scope.$apply() every 500ms. Without it, data doesn't change
    $interval(ng.noop, 500);
}]);
Run Code Online (Sandbox Code Playgroud)

所有这些代码都会导致这样:

工作代码

重要的部分是$scope.data = $window.parent.DataService.getScope();在子控制器中.这就是它获取共享$ scope的地方.

当然,所有这些只有在父和iframe位于同一域下时才有效.否则,它变成了另一个复杂的故事......

希望这会帮助你.


use*_*929 6

好的,所以这是我的解决方案,我希望这是你的想法.

在父应用程序的控制器中:

mainApp = angular.module('mainApp', []);
mainApp.controller('mainCtrl', ['$scope', 'sharedData', function($scope, sharedData){
    $scope.sharedData = sharedData;
    //your controller logic goes here ...
}]);
Run Code Online (Sandbox Code Playgroud)

在iframe应用程序的控制器中:

iframeApp = angular.module('iframeApp', []);
iframeApp.controller('iFrameCtrl', function($scope){
    //here we get the service instance from the parent application, if you 
    //need it in other controllers in the iframe app as well, either get it 
    //there the same way or pass it along via $scope or $rootScope
    var sharedData = window.parent.angular.element(window.frameElement).scope().sharedData;
    //now we can use sharedData the same way as in the parent application controller
Run Code Online (Sandbox Code Playgroud)

});

sharedData.js(共享服务的js文件,只需包括在内parent.html)

mainApp.factory('sharedData', function(){
    var list = [];
    var mainScope;
    var iframeScope;

    //this function needs to be called in all setter methods to update the data in both applications
    function update(){
        if(!mainScope){
            mainScope = angular.element(document.body).scope();
        }
        //$apply() causes errors in the dev console, $applyAsync doesn't, I don't know why
        mainScope.$applyAsync(); 
        if(!iframeScope){
            //the update function might be called before angular is initialized in the iframe application
            if(document.getElementById('iframe').contentWindow.angular){
                iframeScope = document.getElementById('iframe').contentWindow.angular.element(document.body).scope();
                iframeScope.$applyAsync();
            }
        } else {
            iframeScope.$applyAsync();
        }
    }
    return {
        append: function(item) {
            list.push(item); 
            //don't forget to add this at the end of all setter functions in the service
            update();
        },
        getAll: function() { return list }
    }
});
Run Code Online (Sandbox Code Playgroud)

iframes的东西在jsfiddle(也可能是cross-origin)上不起作用,所以我在github页面上放了更广泛的例子:

https://github.com/sammax/angulariframe(代码)

http://sammax.github.io/angulariframe/main/(结果)