komal February 2016

How can i solve "Cannot read property 'length' of undefined at checkWithSeletedModel" in angularjs?

how can i solve this error "Cannot read property 'length' of undefined at checkWithSeletedModel".

when i add object directly in controller its working but when data coming from api i am getting error

TypeError: Cannot read property 'length' of undefined
at checkWithSeletedModel (newbooking.html:404)
at new MyCtrlFun (newbooking.html:391)
at invoke (angular-1.0.1.js:2795)
at Object.instantiate (angular-1.0.1.js:2805)
at angular-1.0.1.js:4620
at angular-1.0.1.js:4201
at forEach (angular-1.0.1.js:117)
at nodeLinkFn (angular-1.0.1.js:4186)
at compositeLinkFn (angular-1.0.1.js:3838)
at compositeLinkFn (angular-1.0.1.js:3841)

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

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
myApp.controller("MyCtrl", MyCtrlFun);
MyCtrlFun.$inject = ['$scope', '$filter', '$timeout','$http'];
function MyCtrlFun($scope, $filter, $timeout,$http) {

//****** Selected List****
$scope.selectedlist = ["Lumia 735 TS", "Lumia 510"];

$scope.checkbox_model = [];
$scope.checkbox_list = [];

$scope.submitModel = [];
var submodel = [];
var models = {};
var brandname = {};
$scope.finalOutput = {
		city:null,
    Storename : null,
    Offerid : null,
    Offer_price : null,
    Total_price : null,
    Sub_Total_price : null,
    Categories : []
}

 
$scope.finalOutput_withNullFilter = {};

 
  
$scope.checkItems = function(){
  $scope.checkbox_list = [];
  $scope.selectedlist = new Array();
  
  for(var item in $scope.checkbox_model){
		if($scope.checkbox_model[item].isChecked)
    {
    		$scope.selectedlist.push($scope.checkbox_model[item].name);
    		$scope.checkbox_list.push($scope.checkbox_model[item]);
    }
    
    
  }
  $scope.checkbox_list.push({offer_id : $scope.selected_        

Answers


ashfaq.p February 2016

You have kept a for loop like this:

for(var i=0; i<$scope.phones.length; i++)

here you are trying to access the length of $scope.phones variable. But you have defined $scope.phones inside the success callback of your api call. But your api calls are all failing. therefore success callback are never getting called and therefore $scope.phones is always undefined. Thus you are getting error that `cannot read length of undefined'.

Post Status

Asked in February 2016
Viewed 3,593 times
Voted 13
Answered 1 times

Search




Leave an answer