简体   繁体   English

AngularJS控制器/工厂返回未定义的对象

[英]AngularJS controller/factory returns undefined object

Angular controller result undefined after factory restful web api call. 工厂restful web api调用后,角度控制器结果未定义。 I've seen many posts about this but none seem to solve my problem. 我看过很多关于这个的帖子,但似乎都没有解决我的问题。 I have a controller that makes a restful web api2 call. 我有一个控制器,可以进行一次宁静的web api2调用。 Using fiddler I can see the call made and the 200 response but I can never seem to get the results, and I know the get is via async. 使用fiddler我可以看到调用和200响应,但我似乎永远无法得到结果,我知道get是通过异步。 Below is the factory and controller. 以下是工厂和控制器。 I've also tried to explicitly send the callback into the factory as well, this approach leads to not even an undefined object result, just doesn't work. 我也尝试将回调显式发送到工厂,这种方法甚至不会导致未定义的对象结果,只是不起作用。 I'm really at a standstill here and I'm new to Angularjs, what is wrong? 我真的处于停滞状态,我是Angularjs的新手,出了什么问题? Currently I have the controller call wired to a button for explicit testing. 目前我将控制器调用连接到一个按钮进行显式测试。

**FACTORY NO EXPLICIT CALL-BACK** 
casenoteApp.factory('MyFactory', function ($http) {


var factory = {};

var urlBase = 'http://xxxxxx/api/Client';


factory.getClients = function (userLogin) {

    return $http.get(urlBase + '/' + userLogin);
};


return factory;
});

**CONTROLLER NO EXPLICIT CALL-BACK** 
casenoteApp.controller('MyController', MyController);

function MyController($scope, MyFactory) {


$scope.addCustomer = function () {

    MyFactory.getClients(123456)
       .success(function (clients) {

            var curCust = clients;
            $scope.status = clients.last_name;

        })
       .error(function (error) {
           $scope.status = 'Unable to load client data: ' + error.message;
       });        

}


}

**FACTORY PASSING IN CALL-BACK**
casenoteApp.factory('MyFactory', function ($http) {


var factory = {};

var urlBase = 'http://xxxxxx/api/Client';

factory.getClients = function (userLogin, callback) {

    return $http.get(urlBase + '/' + userLogin).success(callback)

}

return factory;
});


**CONTROLLER PASSING IN CALL-BACK**
casenoteApp.controller('MyController', MyController);

function MyController($scope, MyFactory) {


$scope.addCustomer = function () 



    MyFactory.getClients(123456, function(clients) {

            var curCust = clients[0];
            $scope.status = clients.last_name;
        })


}

}

Setup your factory to use .then and return the data, like so: 设置你的工厂使用.then ,返回数据,如下所示:

casenoteApp.factory('MyFactory', function ($http) {
    var urlBase = 'http://xxxxxx/api/Client';

    return {
        getClients: function (userLogin) {
            return $http.get(urlBase + '/' + userLogin).then(function(data) {
                return data.result;
            });
        }
    };
});

And your controller: 而你的控制器:

MyFactory.getClients(01234).then(function(data) {
    console.log(data); //hello data!
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM