如何使用jquery从角度服务获取json数据

时间:2022-08-28 16:22:15

I am setting data from a angular controller to a service. I need to somehow get this data using jquery or javascript.Is this possible since I am not requesting it from a url?

我正在将数据从角度控制器设置为服务。我需要以某种方式使用jquery或javascript获取此数据。这是可能的,因为我没有从URL请求它?

Angular service

    getSeriesData: function () {
        return this.legendSeries;
    },
    setSeriesData: function (legendSeries) {
        this.legendSeries = legendSeries;
    },

2 个解决方案

#1


1  

Not sure why you would want to do that (if you explain the scenario better it might help), but in case you want to share data between an angular application and other JS code (e.g. JSON), one way you could go about is to dispatch a global event from the angular service [for example $(window).trigger('myEvent', myJSON] and then catch this on your separate JS code [window.addEventListener('myEvent'...]

不知道为什么你会这样做(如果你更好地解释它可能有帮助),但是如果你想在角度应用程序和其他JS代码(例如JSON)之间共享数据,你可以采取的一种方法是从角度服务[例如$(window).trigger('myEvent',myJSON]调度一个全局事件,然后在你单独的JS代码上捕获它[window.addEventListener('myEvent'...]

#2


1  

Seems like possible using injector

似乎可能使用注射器

var injector = angular.element('body').injector()

$injector.invoke(function (serviceName) {
    var legendSeries = service.legendSeries;
    service.setSeriesData(legendSeries);
});

#1


1  

Not sure why you would want to do that (if you explain the scenario better it might help), but in case you want to share data between an angular application and other JS code (e.g. JSON), one way you could go about is to dispatch a global event from the angular service [for example $(window).trigger('myEvent', myJSON] and then catch this on your separate JS code [window.addEventListener('myEvent'...]

不知道为什么你会这样做(如果你更好地解释它可能有帮助),但是如果你想在角度应用程序和其他JS代码(例如JSON)之间共享数据,你可以采取的一种方法是从角度服务[例如$(window).trigger('myEvent',myJSON]调度一个全局事件,然后在你单独的JS代码上捕获它[window.addEventListener('myEvent'...]

#2


1  

Seems like possible using injector

似乎可能使用注射器

var injector = angular.element('body').injector()

$injector.invoke(function (serviceName) {
    var legendSeries = service.legendSeries;
    service.setSeriesData(legendSeries);
});