2017-03-22 34 views
1

共享數據我有我的角2的應用程序提供config.json一些外部應用程序。 我需要的切入點爲我的角度2,如:如何與外部應用

angular2Application.setConfig('../config.json'); // in browser console e.g. 

(然後我可以在任何我angular2應用的服務使用JSON)

,也是我的應用程序應該能夠從發送一些數據服務的任何外部應用程序通過調用一些方法,如:

var config = angular2Application.getConfig(); // in external other JS application 

大概我應該通過全局窗口進行外部呼叫?任何幫助和建議或例子將讚賞。

,其具有全局配置

我的角2的應用程序的服務來共享。 「另一個js應用程序」 - 這意味着另一個js應用程序在同一個窗口中。

import {Injectable} from "@angular/core"; 
 
import {Observable} from "rxjs"; 
 
import {SomeService} from "../../some.service"; 
 

 
@Injectable() 
 
export class SharingService { 
 
    
 
    public config; // the external app should be able to get this config 
 

 
    constructor(public someService: SomeService) { 
 
    this.someService.dataChanged.subscribe(
 
     (data) => { 
 
     this.config = data; 
 
     } 
 
    ); 
 
    } 
 

 
}

+0

您需要定義「外部應用程序」,因爲該方法不會是相同的,如果T的服務器,或在同一窗口/其它JS應用在分隔的一個,也可能在同一臺計算機上運行的一些隨機的應用程序。 .. – n00dl3

+0

謝謝!這是另一個在同一個窗口中的js應用程序。 – Lemmy

+0

@ n00dl3我認爲他有兩個或更多的應用程序/庫在同一時間運行,他想使用另一個的結果。 –

回答

0

您可以使用類似socket.io並創建兩個應用之間的套接字通信。

+0

謝謝。但是,我可以通過套接字傳遞多大的json文件? – Lemmy

+0

我不認爲有大小的限制。我嘗試在這個[demo](https://socket.io/demos/chat/)上發送'jquery'的源代碼,並且沒有發生問題:)您還可以將JSON文件字符串化並解析爲字符串以進行通信 –