0

I can not understand what is the flow of my application. In my index.html the attribute of my object recovers well, but in my controller it gives me an indefinite result.

Controller.js

angular
    .module('app.core')
    .controller('Controller', Controller);
function Controller($scope, Service) {
    $scope.configs = [];
    var getConfigs = function() {
        Service.getAll.go({
        }).$promise.then(function(successResponse) {
            $scope.configs = successResponse;
            console.log($scope.configs.name); // => Undefined
        }, function(err) {
            $scope.isLoading = false;
            //Function error
        });
    };
    getConfigs();
    console.log($scope.configs.name); // => Undefined
}

Index.html

<!DOCTYPE html>
<head>
    <title>test</title>
</head>
<body ng-app="app.core">
    <div ng-controller="Controller">
        <table >
            <tr>
                <th >Name</th>
                <th>Last Name Urgencia</th>
            </tr>
            <tr ng-repeat="cfg in configs">
                <td >{{cfg.name}}</td>
                <td >{{configuracion.lastName}}</td>
            </tr>
        </table>
    </div>
</body>
</html>

In index.html I can see the value of the property name and surname. console.log, print undefined

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Cristian
  • 111
  • 7
  • 1
    It seems `$scope.configs` is an array (since you're iterating it with `ng-repeat="cfg in configs"`) so you'd at least need something like `console.log($scope.configs[0].name)` – Phil Mar 11 '19 at 22:14
  • 1
    Also, `getConfigs()` is asynchronous so your last `console.log()` will run before it completes – Phil Mar 11 '19 at 22:15
  • Thank you very much Phill, your answer solved my mistake. Thank you – Cristian Mar 11 '19 at 22:23

1 Answers1

0

"cfg in configs" loops through an array but you are logging $scope.configs.name, seeing configs is an array it will not have a property called name. Log $scope.configs[0].name in the callback fucntion and it will log the name property of the first element of the array.

The second console.log is called straight after getConfigs so the callback has not yet run and $scope.configs will still be the empty array that it was initialised to.

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60