0

I have written method called getChildren to get data related to id, but I am unable to access that data in caller method. My sample code is as follows

I have written these two methods in controller and Passed all objects which are necessary

var baseMethod = function(rootNode) {
var id = 123;

var result = getChildren(id);
};


var getChildren = function(id) {
var deferred = $q.defer();

return dummyService.oneAction(id).then(
    function (data) {
        return deferred.resolve(data);
    }
);
}

Whats wrong with this. I want to assign data in callback to my result. How can I do that.

ProxyTech
  • 1,105
  • 8
  • 19
IfOnly
  • 540
  • 1
  • 4
  • 17

2 Answers2

0

You function getChildren return a promise, so try

var baseMethod = function(rootNode) {
  var id = 123;
  var result;

  getChildren(id).then(function(data) {
    result = data;
  });
};
  • I tried this. It is not working. My flow of execution goes forward, but callback is not got called. So I got undefined in result. – IfOnly Jun 25 '14 at 12:25
0
var result = getChildren(id).then(function(data){

angular.copy(data, $scope.child)
});
sylwester
  • 16,498
  • 1
  • 25
  • 33
  • My flow of execution continues,but my callback didn't get called. So I am getting 'undefined' in $scope.child when I try to access it – IfOnly Jun 25 '14 at 12:32
  • Still same problem. If I try to use $scope.child just after this call, I get 'undefined' error. This is because callback is still not executed – IfOnly Jun 25 '14 at 12:51
  • Yes I did that at start of the controller – IfOnly Jun 25 '14 at 15:44