0

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

Toni Chaz
  • 651
  • 11
  • 22
  • 2
    You don't actually get() data – Boris Zagoruiko 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](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron). – 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 – Boris Zagoruiko Nov 26 '14 at 16:27
  • And you console log user, while you should use $scope.user within the controller (or User if you intend to log the service). Ofcourse it's undefined. – alou Nov 26 '14 at 16:33

4 Answers4

0

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 .....

Konammagari
  • 364
  • 2
  • 8
0

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

});
tcase360
  • 366
  • 3
  • 11
0

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);


});
Toni Chaz
  • 651
  • 11
  • 22
  • 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 05: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
0

Use settimeout will not auto fire the $digest and the 2 way data binding may not aware of the update.

  1. user q and promise instead of timeout
  2. if timeout is required, use $timeout instead of settimeout.
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Frank
  • 1