0

I'm working on an mobile app and I'm having problems with getting data to the controller. What i do is i get the data from a service and that works well (on routing this is '/' or root directory):

var listdnModule = angular.module('listdn', []);

listdnModule.factory('getActiveDnService', ['$http', function ($http) {
    return {
        getActiveDnSvc: function (izvajalecID) {
            return $http({ method: 'POST', url: 'svc.aspx/getActiveDN', data: "{'izvajalecID':" + "'" + izvajalecID + "'}", cache: true });
        }
    };
}]);

listdnModule.controller('listdnCtrl', ['$scope', '$http', 'getActiveDnService','$rootScope', function ($scope, $http, svc, $rootScope) {
    $scope.mjav = 1;
    svc.getActiveDnSvc($scope.mjav).success(function (data) {
        $rootScope.dnlist = data.d;
        $scope.listdn = data.d;
    });
}]);

So this works fine. It sets the data to the rootScope and to localscope and reads it from local scope.

Then i'm linking the list of Costumers to costumerdetail. The route settings are like this:

mSvc.config(['$routeProvider',
  function ($routeProvider) {
      $routeProvider.
        when('/', {
            templateUrl: 'tmpl/listdn.html',
            controller: 'listdnCtrl'
        }).
        when('/settings', {
            templateUrl: 'tmpl/nastavitve.html',
            controller: 'settings'
        }).
        when('/dndetail/:dnid', {
            templateUrl: 'tmpl/dndetail.html',
            controller: 'dndetailCtrl'
        }).
        otherwise({
            redirectTo: '/'
        });
  }]);

The error comes after i try to get dndetail. I get the right ID from $routeParams but i dont get the data in the right time. The code is this:

var dndetail = angular.module('dndetail', []);

dndetail.controller('dndetailCtrl', ['$rootScope', '$scope', '$routeParams', function ($rootScope, $scope, $routeParams) {
    console.log($rootScope.dnlist[0]);
    $scope.datal = $rootScope.dnlist;
    $scope.id = $routeParams.dnid;
    for(var i = 0; i <= datal.length; i++) {
        if (datal[i].ID === $scope.id) {
            $scope.data = datal[i];
        }
    }
}]);

And this is the error i get:

Error

As you can see the console.log gets the object and prints the output (I masked it because it's company data) while datal is undefined. The special AngularJS tab in chrome DevTools also says the data is there:

Data is there

And for the end the template of the view that doesnt get data:

<ul ng-controller="dndetailCtrl" class="list-group">
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Naslov</div>
            <div class="pull-right">
                <a ng-href="https://maps.google.com/?q={{ data.Ulica }} {{ data.Posta }} {{ data.Kraj }}">
                    {{ $root.dnlist[1].Ulica }}<br />
                    {{ data.Posta }} {{ data.Kraj }}<br />
                    {{ data.Drzava }}</a>
            </div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Datum izvedbe</div>
            <div id="kupec-pe" class="pull-right">{{ data.DatumIzvedbe }}</div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Datum naloga</div>
            <div id="kupec-dst" class="pull-right">{{ data.DatumNaloga }}</div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Opis:</div>
            <div id="kupec-komerc" class="pull-right">{{ data.Opis }}</div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Šifra dejavnosti:</div>
            <div id="kupec-sif" class="pull-right">{{ data.sifDej }}</div>
        </div>
    </li>
    <li class="list-group-item">
        <div class="row info-row-li">
            <div class="pull-left">Šifra dejavnosti:</div>
            <div id="Div1" class="pull-right">{{ data.DatumIzvedbe }}</div>
        </div>
    </li>
</ul>

Any ideas?

UPDATE

This is the new controller:

dndetail.controller('dndetailCtrl', ['$rootScope', '$scope', '$routeParams', function ($rootScope, $scope, $routeParams) {
    $rootScope.dnlist;
    $scope.id = $routeParams.dnid;
        for (var i = 0; i <= $rootScope.dnlist.length; i++) {
            if ($rootScope.dnlist[i].ID === $scope.id) {
                $scope.data = $rootScope.dnlist[i];
            }
        }
}]);

still doesnt work. From an array of elements i want to get one element by id and then save it to data so i can read from data. For loop doesnt get the data (unindentified dnlist) but if i bind {{ $root.dnlist[1].Property }} in the template it works.

Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
Zoneh
  • 192
  • 5
  • 19
  • why $scope.datal = '{' + $rootScope.dnlist + '}'; why not simply assign $scope.datal = $rootScope.dnlist; – Ajay Beniwal Feb 18 '14 at 10:37
  • You are using `data1.length` in the for loop instead of `$scope.data1.length` - hence the error you see in console. – callmekatootie Feb 18 '14 at 10:41
  • I'm sorry that was in because i tried to test the chrome dev tools if it's correct. It doesn't work even if i do what you suggested ( that was the original). :/ – Zoneh Feb 18 '14 at 10:41
  • @callmekatootie If you check again you can see there is no error with length and it's written correctly i guess, didnt edit that. – Zoneh Feb 18 '14 at 10:42
  • Thanks fooby i'll try that now – Zoneh Feb 18 '14 at 10:43
  • You can take a look at promises as predromarce suggests below. If you can see the data in the console but not when the application runs, it seems like the data is not populated when the code tries to access it (which points to the way you handle promises)... – callmekatootie Feb 18 '14 at 10:44
  • I'll try that. Angular.copy didn't work but resolving promises another way doesn't seem like a solution since the first page "/" in route gets and displays the data and by then the data is in the $rootScope. Then when i go to "/dndetail/8" the data doesn't show although console prints it. I'll try to handle it differently, maybe I should also try to use a service instead of $rootScope. I'll get back asap. – Zoneh Feb 18 '14 at 10:52

2 Answers2

0

I don't think problem is fixed by this answer and tried to delete it, I have undelete it as it has been referred in a comment.

$http in angularjs returns a promise, you can't use the promise directly to access the data, check this answer where it answer something very similar.

Community
  • 1
  • 1
pedromarce
  • 5,651
  • 2
  • 27
  • 27
  • I restructured my service to be like the one in the answer but it doesnt fix the problem :/ – Zoneh Feb 18 '14 at 11:35
  • I know is tricky with $https, but could you get a plunker with your issue? – pedromarce Feb 18 '14 at 11:41
  • I can't expose my webservice atm since I am at work so giving you a plunker is a problem :/ – Zoneh Feb 18 '14 at 11:45
  • So I have tried to change my HTML and it shows data if i use this binding: – Zoneh Feb 18 '14 at 11:52
  • So I have tried to change my HTML and it shows data if i use this binding: {{ $root.dnlist[1].Ulica }} therefore it has access to data. What i need now is to call the row of the id provided in the url... The controller sets the data but the for loop cant get the data. I'm not sure what is happening. couldnt edit before cuz 5min passed by – Zoneh Feb 18 '14 at 11:59
  • If it works like that, why do you need the $scope.datal at all in dndetail? Just rewrite the controller to use $rootScope.dnlist? Also, do you need $scope.id? – pedromarce Feb 18 '14 at 12:10
  • Well datal or data it all comes to the same page. I want to get an element by id from the array. It works like that but i manually chose index 1 and called it from the template. What I want is the for loop to get data which it doesnt even if i loop trough $rootScope.dnlist and then add to $scope.data (so i'd get only one result in an array). The problem is that the for loop doesnt get data and I have no clue why. – Zoneh Feb 18 '14 at 12:12
  • I have edited the controller but still doesnt work, chekc the updated question. – Zoneh Feb 18 '14 at 12:13
  • What I mean is to do this: dndetail.controller('dndetailCtrl', ['$rootScope', '$scope', '$routeParams', function ($rootScope, $scope, $routeParams) { console.log($rootScope.dnlist[0]); for(var i = 0; i <= $rootScope.dnlist.length; i++) { if (datal[i].ID === $routeParams.dnid) { $scope.data = $rootScope.dnlist[i]; } } }]); – pedromarce Feb 18 '14 at 12:58
  • As it was on the beginning, console log shows the value but when we get to the for loop: TypeError: Cannot read property 'ID' of undefined It's interesting that it even gets the length of the dnlist but then it cant read on. – Zoneh Feb 18 '14 at 13:04
  • Is there a field "ID" in the data returned from the web service? Isn't it "id" or something else? – pedromarce Feb 18 '14 at 13:05
  • It's ID (it can also be seen in the top screenshot). I'm loosing my head, I'm probabbly gonna do this some other way. AngularJS is hard if you dont know MVC :) – Zoneh Feb 18 '14 at 13:17
0

If anyone was wondering the problem was with the equality operator. If I change === with the one that checks type == i dont get the TypeError: Cannot read property 'ID' of undefined error any more. Since this is the solution to the problem I'd like to have an explanation also. I'm a javascript beginner to be true and if someone got an idea what happened here, I'd very much like to know the explanation.

The new controller that works:

dndetail.controller('dndetailCtrl', ['$rootScope', '$scope', '$routeParams', function ($rootScope, $scope, $routeParams) {
    $rootScope.dnlist;
    $scope.id = $routeParams.dnid;
        for (var i = 0; i <= $rootScope.dnlist.length; i++) {
            if ($rootScope.dnlist[i].ID == $scope.id) {
                $scope.data = $rootScope.dnlist[i];
            }
        }
}]);
Zoneh
  • 192
  • 5
  • 19