Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem in controller with the properties of object. My factory return one object with another object and one function. I can call the function but i can't access in another object properties. Here is my code:

My factory

app.factory('User', function () {

var User = {};

User.get = function () {
   // Call the service... done
   User.data = response.data;        
};

return User;
});

My controller

app.controller('Controller', function ($scope, User) {
$scope.user = User;

console.log(user);  // print correct one object with 
                       the function get and data object with user data

console.log(user.data); // undefined

});

Thanks, and sorry for my english disaster

share|improve this question
2  
You don't actually get() data –  Glen Swift Nov 26 '14 at 16:24
    
i can call get() function, but i can't access to $scope.user.data –  Toni Chaz Nov 26 '14 at 16:26
1  
Field 'User.data' intialized only in User.get() call, and you haven't called it. –  Rasalom Nov 26 '14 at 16:26
1  
Aside from not actually calling get() (which is the only place where you're actually defining .data), you're probably making this mistake. –  hon2a Nov 26 '14 at 16:26
    
You can call it, but there is no such call in your code snippet. User.get() never executes –  Glen Swift Nov 26 '14 at 16:27

3 Answers 3

up vote 0 down vote accepted

Is my error, i call in another controller User.get();, My problem is in time

app.controller('Controller', function ($scope, User) {
   $scope.user = User;

   setTimeout(function(){
       console.log(user.data); // i have data
   }, 5000);


});
share|improve this answer
1  
You're problem isn't solved with setTimeout. You need to use the $promise methodology. In your example code, setTimeout would break if it took more than 5 seconds to return the data from your AJAX call –  Lloyd Banks Nov 27 '14 at 5:29
    
Thanks @LloydBanks i make a callback when getUser service responded, and then i call the init function of my controller. –  Toni Chaz Nov 27 '14 at 12:19
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  Зелёный Dec 7 '14 at 10:45
    
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. –  Morten Mertner Dec 7 '14 at 11:22
1  
Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. –  cdhowie Dec 7 '14 at 11:28

You have two problems. The way you set up your factory is hurting you. So I would return the whole factory so you have that function. You don't have to define User inside the factory because it is the name of the factory (therefore it is an object)

app.factory('User', function () {

return {
    get: function() {
        //return API call
    };
});

Next, you are defining $scope.user and then calling user. You never defined user, just $scope.user. Also, you must call the get function in user to return data. And it will not be

app.controller('Controller', function ($scope, User) {
$scope.user = User.get();

console.log($scope.user);  // This will be the data

});
share|improve this answer

app.factory('User', function () {

var User = {};
  
  User.data=null;

User.get = function () {
   // Call the service... done
   User.data = response.data;        
};

return User;
});

Controller:

app.controller('Controller', function ($scope, User) {
$scope.user = User;

console.log(user);

console.log(user.data); // null,because you did not call the method User.get();
  
  User.get();

  
console.log(user.data); // this will print the response data which you assign in the User.get() method
});

In the code you given, User.data will give the object but before you have to call User.get() function.

Sorry for my English .....

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.