ahmdabos ahmdabos - 1 year ago 104
AngularJS Question

using factory in the controller retrun undefind

i am trying to send value from my view to the controller using function inside

, then use that value to pass it to my factory which goint to pull data from rest api link , but the value im sending return nothing without errors, here is my code

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, idService) {
$scope.getSo = function (id){
$scope.myIDs = idService.getID(id);
app.factory('idService', function($http,$q) {
var oGetID = {};
oGetID.getID = function(id) {
var deferred = $q.defer();

return deferred.promise;
return oGetID;

<div ng-app="myApp" ng-controller="myCtrl">
<a href="#" ng-click ="getSo('52527290c5cddb0a7cb654ca');">send variable</a>

this is the console result of the responseenter image description here

Answer Source

Asynchronous call doesn't work the way you are expecting here. Then happens asynchronously and they return return the data based on API server call. You should expect value after a ajax succeed, by putting .then over it. As internally $http method's return promise.

   $scope.myIDs = response;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download