Javascript 如何在angular js中从服务返回值?

Javascript 如何在angular js中从服务返回值?,javascript,angularjs,Javascript,Angularjs,我知道数据来自服务器(我进行了单元测试,并在chrome中的调试器中看到了数据),但我不知道如何将数据从angular服务返回到angular控制器 服务: 已更新 surchargeIndex.service('customerService', [ '$http', function ($http) { this.getTest = function () { return $http({ method: "GET",

我知道数据来自服务器(我进行了单元测试,并在chrome中的调试器中看到了数据),但我不知道如何将数据从angular服务返回到angular控制器

服务:

已更新

surchargeIndex.service('customerService', [
'$http', function ($http) {
    this.getTest = function () {
       return $http({
                method: "GET",
                url: "api/Customer/GetTest",
            })
            .success(function(data) {
                return data;
            });
    };
}
]))

控制器:

surchargeIndex.controller('SurchargeIndexController', function ($scope, customerService, templateService) {
    $scope.customers = customerService.getTest();

});
数据具有来自服务器的数组,因此在服务中填充该数组。因此,重申数据是存在的;但是,在调试期间,我在成功处理程序中收到一个404错误


我缺少什么?

$http
异步工作;幸运的是,它返回了一个承诺,当从服务器检索到响应时,该承诺将得到实现。所以您应该返回$http的get方法,并使用返回的promise来处理数据

this.getTest = function () {
        return $http({
                method: "GET",
                url: "api/Customer/GetTest",
            })
            .success(function(data) {
                return data;
            })
            .error(function() {
                alert("failed");
        }); // This returns a promise

    };
然后在控制器中,您应该使用该承诺来检索预期的数据

surchargeIndex.controller('SurchargeIndexController', function ($scope, customerService, templateService) {
    //Use the returned promise to handle data, first parameter of the promise is used for successful result, if error happens, second parameter of the promise returns the error and you can do your error handling in that function
    customerService.getTest().then(function(customers){$scope.customers = customers;},   function(err){console.error(err);})
});

$http
异步工作;幸运的是,它返回了一个承诺,当从服务器检索到响应时,该承诺将得到实现。所以您应该返回$http的get方法,并使用返回的promise来处理数据

this.getTest = function () {
        return $http({
                method: "GET",
                url: "api/Customer/GetTest",
            })
            .success(function(data) {
                return data;
            })
            .error(function() {
                alert("failed");
        }); // This returns a promise

    };
然后在控制器中,您应该使用该承诺来检索预期的数据

surchargeIndex.controller('SurchargeIndexController', function ($scope, customerService, templateService) {
    //Use the returned promise to handle data, first parameter of the promise is used for successful result, if error happens, second parameter of the promise returns the error and you can do your error handling in that function
    customerService.getTest().then(function(customers){$scope.customers = customers;},   function(err){console.error(err);})
});

$http
异步工作;幸运的是,它返回了一个承诺,当从服务器检索到响应时,该承诺将得到实现。所以您应该返回$http的get方法,并使用返回的promise来处理数据

this.getTest = function () {
        return $http({
                method: "GET",
                url: "api/Customer/GetTest",
            })
            .success(function(data) {
                return data;
            })
            .error(function() {
                alert("failed");
        }); // This returns a promise

    };
然后在控制器中,您应该使用该承诺来检索预期的数据

surchargeIndex.controller('SurchargeIndexController', function ($scope, customerService, templateService) {
    //Use the returned promise to handle data, first parameter of the promise is used for successful result, if error happens, second parameter of the promise returns the error and you can do your error handling in that function
    customerService.getTest().then(function(customers){$scope.customers = customers;},   function(err){console.error(err);})
});

$http
异步工作;幸运的是,它返回了一个承诺,当从服务器检索到响应时,该承诺将得到实现。所以您应该返回$http的get方法,并使用返回的promise来处理数据

this.getTest = function () {
        return $http({
                method: "GET",
                url: "api/Customer/GetTest",
            })
            .success(function(data) {
                return data;
            })
            .error(function() {
                alert("failed");
        }); // This returns a promise

    };
然后在控制器中,您应该使用该承诺来检索预期的数据

surchargeIndex.controller('SurchargeIndexController', function ($scope, customerService, templateService) {
    //Use the returned promise to handle data, first parameter of the promise is used for successful result, if error happens, second parameter of the promise returns the error and you can do your error handling in that function
    customerService.getTest().then(function(customers){$scope.customers = customers;},   function(err){console.error(err);})
});

在异步http调用之后,您需要定义回调以将数据“返回”到控制器。。。有不同的方法做。。。我将向您展示一种没有回调或承诺的方法,但最好的方法是使用回调或承诺

野生西路:

app.controller('myCTRL', function($scope, myService) {

       $scope.valueWanted = myService.valueWanted;
       myService.getData();

});

app.service('myService', function($http) {

       var myThis = this;

       this.valueWanted = "";
       this.getData = function () {
              $http.get('api/Customer/GetTest').success(function (data) {
                     myThis.valueWanted = data.valueWanted;
              });
       };

});

在异步http调用之后,您需要定义回调以将数据“返回”到控制器。。。有不同的方法做。。。我将向您展示一种没有回调或承诺的方法,但最好的方法是使用回调或承诺

野生西路:

app.controller('myCTRL', function($scope, myService) {

       $scope.valueWanted = myService.valueWanted;
       myService.getData();

});

app.service('myService', function($http) {

       var myThis = this;

       this.valueWanted = "";
       this.getData = function () {
              $http.get('api/Customer/GetTest').success(function (data) {
                     myThis.valueWanted = data.valueWanted;
              });
       };

});

在异步http调用之后,您需要定义回调以将数据“返回”到控制器。。。有不同的方法做。。。我将向您展示一种没有回调或承诺的方法,但最好的方法是使用回调或承诺

野生西路:

app.controller('myCTRL', function($scope, myService) {

       $scope.valueWanted = myService.valueWanted;
       myService.getData();

});

app.service('myService', function($http) {

       var myThis = this;

       this.valueWanted = "";
       this.getData = function () {
              $http.get('api/Customer/GetTest').success(function (data) {
                     myThis.valueWanted = data.valueWanted;
              });
       };

});

在异步http调用之后,您需要定义回调以将数据“返回”到控制器。。。有不同的方法做。。。我将向您展示一种没有回调或承诺的方法,但最好的方法是使用回调或承诺

野生西路:

app.controller('myCTRL', function($scope, myService) {

       $scope.valueWanted = myService.valueWanted;
       myService.getData();

});

app.service('myService', function($http) {

       var myThis = this;

       this.valueWanted = "";
       this.getData = function () {
              $http.get('api/Customer/GetTest').success(function (data) {
                     myThis.valueWanted = data.valueWanted;
              });
       };

});

您说它返回一个“承诺”,这意味着什么?您可能知道javascript是异步工作的,因此当您通过
$http
发出请求时,流程不会停止,直到它返回一个结果,而是继续执行下一个命令。通过回复承诺,你给了打电话的人一个钩子。当从服务器检索到响应时,承诺将得到履行,并且将使用承诺的
然后
功能通知客户端。您可能需要检查哪一个是广泛使用的promise实现。好的,所以数据包含数组,但不会返回到控制器。我在成功处理程序中获得了一个,但当我调试它时,我在成功处理程序内部收到了一个404 on返回数据404,这意味着请求的路径“api/Customer/GetTest”在请求的服务器中不存在,您可以检查该路径吗?您可以从http客户端(如postman)发出请求吗?您需要控制器中cubbuk描述的
.then()
。如果没有它,承诺永远不会到达您的控制器。您说它返回一个“承诺”,这意味着什么?正如您可能知道的那样,javascript是异步工作的,因此当您通过
$http
发出请求时,过程不会停止,直到它返回一个结果,而是继续执行下一个命令。通过回复承诺,你给了打电话的人一个钩子。当从服务器检索到响应时,承诺将得到履行,并且将使用承诺的
然后
功能通知客户端。您可能需要检查哪一个是广泛使用的promise实现。好的,所以数据包含数组,但不会返回到控制器。我在成功处理程序中获得了一个,但当我调试它时,我在成功处理程序内部收到了一个404 on返回数据404,这意味着请求的路径“api/Customer/GetTest”在请求的服务器中不存在,您可以检查该路径吗?您可以从http客户端(如postman)发出请求吗?您需要控制器中cubbuk描述的
.then()
。如果没有它,承诺永远不会到达您的控制器。您说它返回一个“承诺”,这意味着什么?正如您可能知道的那样,javascript是异步工作的,因此当您通过
$http
发出请求时,过程不会停止,直到它返回一个结果,而是继续执行下一个命令。通过回复承诺,你给了打电话的人一个钩子。当从服务器检索到响应时,承诺将得到履行,并且将使用承诺的
然后
功能通知客户端。您可能需要检查哪一个是广泛使用的promise实现。好的,所以数据包含数组,但不会返回到控制器。我在成功处理程序中获得了一个,但当我调试它时,我在成功处理程序内部收到了一个404 on返回数据404,这意味着请求的路径“api/Customer/GetTest”在请求的服务器中不存在,您可以检查该路径吗?您可以从http客户端(如postman)发出请求吗?您需要控件中cubbuk描述的
.then()