1

I'm trying to assign an object through a service to a $scope object. However I get an error saying that the object is undefined. I'm making an asynchronous call to get the object and below I've used $timeout service to simulate the same. I've tried $q.defer() and returned a promise suspecting it to be a timing issue and also called $apply() to invoke binding but nothing seems to be working. Below is the link to the code in plunker. Pls help me out with this. http://plnkr.co/edit/wyF4Bx1a39IEWS3m8Loe?p=info

<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="profctrl">
  <p>Hello {{usr}}!</p>
</body>

</html>

var ub = angular.module('app', []);
ub.controller('profctrl', ["$scope", "formsub", "$log", "$timeout", function($scope, formsub, $log, $timeout) {
  $log.log("In Profile controller");
  $scope.msg = "";
  $scope.usr = {};

  var fbresponse = {
    name: 'xyz',
    email: '[email protected]'
  };

  $scope.usr = formsub.getprof(fbresponse);
  $timeout(function() {
    $log.log('$scope.usr', $scope.usr);
  }, 2000);
}]);

ub.service('formsub', ["$http", "$log", "$q", "$timeout", function($http, $log, $q, $timeout) {
  var user = {};
  var self = this;

  self.msg = "";


  self.getprof = function(user) {

      $timeout(function() {
        $log.log('user assigned');
        return user;
      }, 2000);

    } //getprof

}]);
2

4 Answers 4

1
var ub = angular.module('myApp', []);
ub.controller('profctrl', ["$scope", "formsub", "$log", "$timeout", function($scope, formsub, $log, $timeout) {
$log.log("In Profile controller");
$scope.msg = "";
$scope.usr = {};

var fbresponse = {
name: 'xyz',
email: '[email protected]'
};
formsub.getprof(fbresponse).then(function(result) {
$scope.usr = result;
alert(JSON.stringfy(result));
});

}]);

ub.service('formsub', ["$http", "$log", "$q", "$timeout", function($http, $log, $q, $timeout) {
 var user = {};
 var self = this;
 self.msg = "";
 self.getprof = function(user) {
  var deferral = $q.defer();
  $timeout(function() {
    $log.log('user assigned');
    deferral.resolve(user);
  }, 2000);
  return deferral.promise;
} //getprof
}]);

Demo

I have use the $q.I think this will help you

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Tojo. That helped!! I tried it earlier but as below. thats why I had an issue. $scope.usr = formsub.getprof(fbresponse).then()
1

Module name is app instead of plunkr in the html ng-app

1 Comment

Thanks Ciril. Apologies for the miss,but that was not my issue.
1

Firstly, You have given the wrong module name, it should be ng-app="app". Now if you use $q it will work correctly. here is plunkr

2 Comments

Thanks Kunal. That helped!! I tried it earlier but as below. thats why I had an issue. $scope.usr = formsub.getprof(fbresponse).then()
@RajkumarKaliyaperumal what exactly do you want to implement? can you briefly clarify?
0

You should be using promise to get your data from $http as the ajax call is asynchronous. You could either use $q or return the $http itself which is inturn a defferred object.

Service:

  self.getprof = function(user) {
  var deff = $q.defer();
  //could be get/post/put...
    $http.post(your_api_url, user).
     then(
          function(data){ deff.resolve(data);   },
          function(ex) { deff.reject(ex); }
       );
         deff.promise;

    } 

And in controller:

formsub.getprof(user).
  then(
          function(data){
                 // success call back 
                $scope.usr = data;
             },
          function(ex) { // error call back. Handle error here}
       );

2 Comments

Thanks . That helped!! I tried it earlier but as below. thats why I had an issue. $scope.usr = formsub.getprof(fbresponse).then()
@Rajkumar - Glad I helped. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.