Open a new window, run the $ scope function in this window, the best way

So, I have two applications with one page, these two applications contain a list of elements, in Application Awe have List A, and in Application Bwe have List B.

an element in List Acan be a parent for elements in List B, from one to many relationships.

If the user clicks the item in App A, he gets the opportunity view related items, it will open App Bin a separate window (new tab) and filters List Bto display only related items by clicking the item in App A.

This means that I have to open a new window and run the function when this window is ready, and pass the IDclicked item to filter my list.

$scope.openNewWindow = function(id){

    var popup = window.open('newPageUrl');

      // check to see when opened page is ready
    var readyStateCheckInterval = setInterval(function() {
        if (popup.document.readyState === "complete") {
            clearInterval(readyStateCheckInterval);
            // function on App B
            popup.functionToRunWhenReady(id);
        }
    }, 50);
};

functionToRunWhenReady() javascript , $scope, , $scope

function functionToRunWhenReady(id) {
    angular.element(document).ready(function () {

        var scope = angular.element($('#myAppDiv')).scope();

        scope.$apply(function() {
             // scope function to filter the list
            scope.setFilter(id);
        });
    });
}

, , document, scope, var scope = angular.element($('#myAppDiv')).scope();, setFilter(id)

, , .

tl; dr , $scope , ?

: , $routeparams, , ( ),

+4
3

GET B. - $routeParams.

+3

A . target="blank" .

<a href="http://applicationB_url/{{itemA.id}}/items" target="blank">{{itemA.name}}</a>

B, , $stateParams ( ui router) $routeParams .

, Application B. - :

if($stateParams.id) {
   // do something
}
+2

var popup = window.open('newPageUrl?id=' + id);

- id URL:

var search = document.location.href;
var id = JSON.parse('{"' + decodeURI(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}')['id'];

A better alternative would be to save the data as a cookie cookiesave('id', id); and then var id = cookieget('id');

+2
source

All Articles