Yesterday i had a problem with my mobile app. I'm using angularjs and at first I thought it was angulars fault so i tried everything that could be tried. Well in the end the solution was to change equality operator from ===
to ==
(type checking). Well I was totaly confused by what happened. I dont know much about javascript but this I gotta know. Since my old SO question (AngularJS How to get data to controller via $rootScope?) has only angularjs tag i'm bringing this question to javascript.
Why would this code (with ===
) return error "TypeError: Cannot read property 'ID' of undefined"
:
dndetail.controller('dndetailCtrl', ['$rootScope', '$scope', '$routeParams', '$localStorage', function ($rootScope, $scope, $routeParams, $localStorage) {
var mjav = [];
$scope.$storage = $localStorage;
var lslist = $scope.$storage.dnlist;
var id = $routeParams.dnid;
$scope.$storage.test1 = $scope.$storage.dnlist[1].ID;
for (var i = 0; i <= $scope.$storage.dnlist.length; i++) {
if ($scope.$storage.dnlist[i].ID === id) {
$scope.data = $scope.$storage.dnlist[i];
break;
}
}
//$scope.data = $rootScope.dnlist[$rootScope.getIndex($routeParams.dnid)];
}]);
And the code with ==
works as expected. I'm a relatively new Javascript user (3months), before I had some experience with VB/C#.net.
EDIT: forgot to mention I have tested both variables with typeof and they both return integer.