AngularJS:来自服务的异步$ http.jsonp调用

use*_*583 2 javascript ajax asynchronous angularjs

我正在尝试编写一个执行以下操作的简单应用程序:1.用户输入2个参数并单击按钮2. Angular调用返回JSON 3的外部JAVA Servlet.应用程序将json字符串输出到屏幕

我有一个问题,因为当我点击按钮没有任何反应.我相信(由于测试)发生这种情况的原因是,由于调用是异步的,因此返回的变量为null.

相关代码:

controllers.js

function myAppController($scope,kdbService) {
    $scope.buttonClick = function(){
        var dat = kdbService.get($scope.tName,$scope.nRows);
        $scope.data = dat;

    }
}
Run Code Online (Sandbox Code Playgroud)

services.js

angular.module('myApp.services', []).factory('kdbService',function ($rootScope,$http){
    var server="http://localhost:8080/KdbRouterServlet";
    return {
        get: function(tname,n){
            var dat;
            $http.jsonp(server+"?query=krisFunc[`"+tname+";"+n+"]&callback=JSON_CALLBACK").
                success(function(data, status, headers, config) {
                    console.log("1");
                    console.log(data);
                    dat=data;
                }).
                error(function(data, status, headers, config) {
                    alert("ERROR: Could not get data.");
                });
            console.log("2");
            console.log(dat);
            return dat;
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

的index.html

<!-- Boilerplate-->
<h1>Table Viewer</h1>
<div class="menu" >
    <form>
        <label for="tName">Table Name</label>
        <input id="tName" ng-model="tName"><br>
        <label for="nRows">Row Limit</label>
        <input id="nRows" ng-model="nRows"><br>
        <input type="submit" value="Submit" ng-click="buttonClick()">
    </form>
</div>
{{data}}
<!-- Boilerplate-->
Run Code Online (Sandbox Code Playgroud)

当我执行代码并按下按钮时,没有任何反应.但是,如果我查看我的日志,我会看到:

2
undefined
1 
Object {x: Array[2], x1: Array[2]}
Run Code Online (Sandbox Code Playgroud)

显然,正在发生的是在get函数返回后,success函数返回.因此,放入$ scope.data的对象是未定义的,但是从jsonp调用返回的对象被遗忘.

有没有正确的方法来做到这一点?大多数教程我看到的数据分配到$ scope变量里面成功的功能,从而跳过这个问题.如果可能的话,我希望我的服务能够脱离.

任何帮助,将不胜感激.

mpm*_*mpm 7

我会做那样的事情:

调节器

function myAppController($scope,kdbService) {
    $scope.kdbService = kdbService;
    $scope.buttonClick = function(){
        $scope.kdbService.get($scope.tName,$scope.nRows);

    }
}
Run Code Online (Sandbox Code Playgroud)

服务

angular.module('myApp.services', []).factory('kdbService',function ($rootScope,$http){
    var server="http://localhost:8080/KdbRouterServlet";
    return {
        data:{},
        get: function(tname,n){
            var self = this;
            $http.jsonp(server+"?
            query=krisFunc[`"+tname+";"+n+"]&callback=JSON_CALLBACK").
                success(function(data, status, headers, config) {
                    console.log("1");
                    console.log(data);
                    self.data = data;
                }).
                error(function(data, status, headers, config) {
                    alert("ERROR: Could not get data.");
                });
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

HTML

{{kdbService.data}}
Run Code Online (Sandbox Code Playgroud)

要么

在get方法中使用continuation:

调节器

function myAppController($scope,kdbService) {
    $scope.buttonClick = function(){
        kdbService.get($scope.tName,$scope.nRows,function success(data){
           $scope.data = data;
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

服务

    get: function(tname,n,successCallback){
        $http.jsonp(server+"?query=krisFunc[`"+tname+";"+n+"]&callback=JSON_CALLBACK").
            success(function(data, status, headers, config) {
                successCallback(data,status,headers,config);
            }).
            error(function(data, status, headers, config) {
                alert("ERROR: Could not get data.");
            });
    }
Run Code Online (Sandbox Code Playgroud)

或使用$ resource服务

http://docs.angularjs.org/api/ngResource.$ resource(你需要角度资源模块

代码未经测试.

如果可能的话,我希望我的服务能够脱离.

然后将"数据对象"放在称为"数据提供者服务"的"数据服务"中.无论如何,你必须在某个地方调用"数据提供者服务".在我看来,没有跳过这个问题,因为这就是javascript的工作原理.

也用 angular.controller("name",["$scope,"service",function($s,s){}]);

所以你不需要关心如何调用参数,只要它们被正确定义和注入.