Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从angularjs中的服务回调函数中检索数据?请找到我的程序错误_Javascript_Angularjs - Fatal编程技术网

Javascript 如何从angularjs中的服务回调函数中检索数据?请找到我的程序错误

Javascript 如何从angularjs中的服务回调函数中检索数据?请找到我的程序错误,javascript,angularjs,Javascript,Angularjs,您忘记返回数据: var app = angular.module("myApp",[]); app.controller("searchController",['$scope','searchByNameService', function($scope, searchByNameService) { $scope.searchName = "raj"; $scope.doSearchByName = function () { alert("inside

您忘记返回数据:

var app = angular.module("myApp",[]);

app.controller("searchController",['$scope','searchByNameService', function($scope, searchByNameService) {

    $scope.searchName = "raj";
    $scope.doSearchByName = function () {
        alert("inside controller " + $scope.searchName);

        searchByNameService.searchByName($scope.searchName, function(r) {
            alert("inside service " + r);

            $scope.custno = r.custno;
            $scope.name = r.name;
            $scope.city = r.city;
            $scope.mobile = r.mobile;
            alert("name " + r.name + " " + " city " + r.city + " mobile " + r.city);

        });
    };//service

}])

app.service('searchByNameService',['$http','$log', function($http, $log){

    //$log.log("service instantiation ..");

    this.searchByName = function(custName, cb){
        $http({
            url: 'http://localhost:4545/customers/' + custName,
            method: 'GET'
        }).then(function(resp){
            cb(resp.data) // it does not return data , but it display on console
            $log.log(resp.data);
        }, function(res){
            $log.error('Service :: ERROR OCCURED,OR SERVER PROBLEM....');
            debugger;
        });
    };

}]);//service
this.searchByName=函数(custName,cb){
$http({
网址:'http://localhost:4545/customers/"姓名,,
方法:“获取”
}).然后(功能(resp){
cb(resp.data)//它不返回数据,但显示在控制台上
$log.log(各自的数据);

return resp;//首先,控制台中有任何错误吗?“请查找我的程序错误”不是此网站的工作方式。请阅读。您应该返回您的数据。在控制台上无错误显示输出,如:Array(1)0:{custno:101,name:“raj”,city:“pune”,mobile:95270778,famillymember:“4”,}proto\uuuu:Objectlength:1\uuuu proto\uuuuu:Array(0)如果它登录到您的控制台,那么您可能无法很好地处理返回值。
this.searchByName = function(custName, cb){
        $http({
            url: 'http://localhost:4545/customers/' + custName,
            method: 'GET'
        }).then(function(resp){
            cb(resp.data) // it does not return data , but it display on console
            $log.log(resp.data);
            return resp; // <---------------------------------   Return the data
        }, function(res){
            $log.error('Service :: ERROR OCCURED,OR SERVER PROBLEM....');
            debugger;
        });
    };