javascript - How can i solve "Cannot read property 'length' of undefined at checkWithSeletedModel" in angularjs? -
how can solve error "cannot read property 'length' of undefined @ checkwithseletedmodel".
when add object directly in controller working when data coming api getting error
typeerror: cannot read property 'length' of undefined @ checkwithseletedmodel (newbooking.html:404) @ new myctrlfun (newbooking.html:391) @ invoke (angular-1.0.1.js:2795) @ object.instantiate (angular-1.0.1.js:2805) @ angular-1.0.1.js:4620 @ angular-1.0.1.js:4201 @ foreach (angular-1.0.1.js:117) @ nodelinkfn (angular-1.0.1.js:4186) @ compositelinkfn (angular-1.0.1.js:3838) @ 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_offer_id}); //alert(storename) /*var m = pricechangetooffer($scope.selected_offer_id); if(!isnan(m)){ $scope.total_price =$scope.total_price-parseint(m); }*/ $scope.checkbox_list.push({sub_total : $scope.sub_total}); checkwithseletedmodel(); $scope.finaloutput.city=$scope.phones[0].city; $scope.finaloutput_withnullfilter = $scope.finaloutput; if($scope.finaloutput.storename==null) { $scope.finaloutput.storename = $scope.phones[0].storename; } if($scope.finaloutput.offerid==null) { delete $scope.finaloutput.offerid; } if($scope.finaloutput.offer_price==null) { delete $scope.finaloutput.offer_price; } alert(json.stringify($scope.finaloutput)); //****** redefine finaloutput after submit data server $scope.finaloutput = { city:null, storename : null, offerid : null, offer_price : null, total_price : null, sub_total_price : null, categories : [] } } $scope.selectedbrand = 'nokia'; $scope.submodel ='lumia'; $http.get('http://19.168.1.4/shop1/').success(function(data, status, response) { //$scope.phones = data //console.log($scope.phones); //console getting object .here have added have got in console $scope.phones = [{ "_id": { "$oid": "56a9c44f40d0275643cfc04e" }, "default": [], "categories": [ { "models": [ { "submodel": [ { "price": "not available", "name": "lumia 735 ts" }, { "price": "8100", "name": "lumia 510" }, { "price": "9900", "name": "lumia 830" } ], "name": "lumia" }, { "submodel": [ { "price": "10000", "name": "asha 230" }, { "price": "11999", "name": "asha asn01" } ], "name": "asha" } ], "brandname": "nokia", "id": "986745" }, { "models": [ { "submodel": [ { "price": "5000", "name": "trend 840" }, { "price": "6999", "name": "a5" } ], "name": "galaxy" }, { "submodel": [ { "price": "12000", "name": "asha 230" } ], "name": "asha" } ], "brandname": "samsung", "id": "144745" } ], "storename": "zig zag mobiles", "shopid":"asdef1234", "city":"bengaluru", "__v": 0 } ] }); //----------------------------our shop offers---------------------------------------- $http.get('http://19.168.1.4/offers/').success(function(data, status, response) { //$scope.offers =data; //console.log($scope.offers); //console getting object .here have added have got in console $scope.offers = [ { id: "as23456", "storename": "zig zag mobiles", "shopid":"asdef1234", "offer_message":"1000rs off", "categories": [ { "models": [ { "submodel": [ { "price": "1000", "name": "lumia 735 ts" } ], "name": "lumia" } ], "brandname": "nokia", "id": "986745" } ], },{ id: "de34575", "storename": "zig zag mobiles", "shopid":"asdef1234", "offer_message":"500rs off", "categories": [ { "models": [ { "submodel": [ { "price": "500", "name": "lumia 830" } ], "name": "lumia" } ], "brandname": "nokia", "id": "345645" } ], }, { id: "ert25675", "storename": "zig zag mobiles", "shopid":"asdef1234", "offer_message":"100 flat", "categories": [ { "models": [ { "submodel": [ { "price": "100", "name": "asha 230" } ], "name": "asha" } ], "brandname": "samsung", "id": "345645" } ], } ] }); $scope.offerselectedlist = []; $scope.total_price = 0, $scope.sub_total=0; $scope.changeitemstatefun = function(checkbox_value){ if(checkbox_value.ischecked){ if(!isnan(checkbox_value.price)){ $scope.total_price =$scope.total_price+parseint(checkbox_value.price); } var checked_offer = getoffer([checkbox_value]); if(checked_offer!=false){ $scope.offerselectedlist.push(checked_offer); //alert(checked_offer.categories[0].models[0].submodel[0].name); } }else{ if(!isnan(checkbox_value.price)){ $scope.total_price = $scope.total_price-parseint(checkbox_value.price); } var checked_offer = getoffer([checkbox_value]); if(checked_offer!=false){ var tempofferlist = []; for(var in $scope.offerselectedlist){ if($scope.offerselectedlist[i].id!=checked_offer.id){ tempofferlist.push($scope.offerselectedlist[i]); } } $scope.offerselectedlist = tempofferlist; } } $scope.sub_total = $scope.total_price; $scope.finaloutput.total_price = $scope.total_price; $scope.finaloutput.sub_total_price = $scope.sub_total; } $scope.selected_offer_id = null; $scope.submit_offer = {}; $scope.offerseleted = function(offer){ $scope.selected_offer_id = offer; var m = pricechangetooffer($scope.selected_offer_id); if(!isnan(m.offer_price)){ $scope.sub_total =$scope.total_price-parseint(m.offer_price); } $scope.finaloutput.storename = m.storename; $scope.finaloutput.offerid = m.offerid; $scope.finaloutput.offer_price = m.offer_price; $scope.finaloutput.total_price = $scope.total_price; $scope.finaloutput.sub_total_price = $scope.sub_total; } function getoffer(checkeditem){ for(var i=0; i<checkeditem.length; i++){ for(var j=0; j<$scope.offers.length; j++){ for(var k=0;k<$scope.offers[j].categories.length; k++){ for(var m=0; m<$scope.offers[j].categories[k].models.length; m++){ for(var n=0; n<$scope.offers[j].categories[k].models[m].submodel.length; n++){ if($scope.offers[j].categories[k].models[m].submodel[n].name==checkeditem[i].name){ return $scope.offers[j]; } } } } } } return false; } //----------------------------------------------------------------------------------- checkwithseletedmodel(); function checkwithseletedmodel(){ $scope.finaloutput.categories = []; $scope.submitmodel = []; submodel = []; models = {}; brandname = {}; $scope.offerselectedlist = []; $scope.total_price = 0; $scope.checkbox_model = []; for(var i=0; i<$scope.phones.length; i++){ for(var j=0; j<$scope.phones[i].categories.length; j++){ if($scope.phones[i].categories[j].brandname==$scope.selectedbrand){ brandname = { id : $scope.phones[i].categories[j].id, brandname : $scope.phones[i].categories[j].brandname, models : [] }; models = {}; for(var k=0; k<$scope.phones[i].categories[j].models.length; k++){ if($scope.phones[i].categories[j].models[k].name==$scope.submodel){ models = { name : $scope.phones[i].categories[j].models[k].name, submodel : [] } for(var m=0; m<$scope.phones[i].categories[j].models[k].submodel.length; m++){ var verifyischeck = false; if($scope.selectedlist.length>0){ verifyischeck=verifyischecked($scope.phones[i].categories[j].models[k].submodel[m].name); } if(verifyischeck==true){ models.submodel.push({name : $scope.phones[i].categories[j].models[k].submodel[m].name, price:($scope.phones[i].categories[j].models[k].submodel[m].price=='not available'?null:$scope.phones[i].categories[j].models[k].submodel[m].price)}); //models.submodel } var obj1 = {ischecked :verifyischeck, name : $scope.phones[i].categories[j].models[k].submodel[m].name, price:$scope.phones[i].categories[j].models[k].submodel[m].price}; //$scope.changeitemstatefun(obj1); var checkedoffer = getoffer([obj1]); if(checkedoffer!=false && verifyischeck==true){ $scope.offerselectedlist.push(checkedoffer); } if(verifyischeck==true && !isnan(obj1.price)){ $scope.total_price = $scope.total_price+parseint(obj1.price); } $scope.checkbox_model.push(obj1); } } } brandname.models.push(models); $scope.submitmodel.push(brandname); } } } $scope.finaloutput.categories.push($scope.submitmodel); } function verifyischecked(submodel_name){ for(var i=0; i<$scope.selectedlist.length; i++){ if($scope.selectedlist[i]==submodel_name){ return true; } } return false; } //********** total sum checked model , change according offer function pricechangetooffer(offer_id){ for(var j=0; j<$scope.offers.length; j++){ if(offer_id!=$scope.offers[j].id) continue; for(var k=0;k<$scope.offers[j].categories.length; k++){ for(var m=0; m<$scope.offers[j].categories[k].models.length; m++){ for(var n=0; n<$scope.offers[j].categories[k].models[m].submodel.length; n++){ var offer_obj = { storename : $scope.offers[j].storename, offerid : $scope.offers[j].id, offer_price : $scope.offers[j].categories[k].models[m].submodel[0].price }; return offer_obj; } } } } } $scope.sub_total = $scope.total_price; $scope.finaloutput.total_price = $scope.total_price; $scope.finaloutput.sub_total_price = $scope.sub_total; } myapp.filter("removespace", function(){ return function(input){ return input.replace(/[^a-z0-9]+/ig, "_") } }); //]]> //]]> //]]>
<script type="text/javascript" src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-app="myapp" ng-controller="myctrl"> <div class="item" ng-repeat="item in phones"> <div ng-repeat="cat in item.categories" ng-show="cat.brandname==selectedbrand"> <h2><b>{{ cat.brandname }}</b></h2> <div ng-repeat="models in cat.models" ng-show="models.name==submodel"> <h3> <b>{{models.name}}</b> </h3> <div ng-repeat="submodels in models.submodel"> <input type="checkbox" ng-model="checkbox_model[$index].ischecked" ng-change="changeitemstatefun(checkbox_model[$index])"/>{{ submodels.name }} </div><br/><br/> <b>sub total = {{sub_total}}</b><br/> <select ng-show="offerselectedlist.length>0" ng-model="selected_item" ng-options="offer.id offer.offer_message offer in offerselectedlist" ng-change="offerseleted(selected_item)"> <option>select offer</option> </select> </div> </div> <button ng-click="checkitems()">do</button> </div> </div>
how can solve pls 1 me out move forward have added code below .
you have kept loop this:
for(var i=0; i<$scope.phones.length; i++)
here trying access length of $scope.phones variable. have defined $scope.phones
inside success callback of api call. api calls failing. therefore success callback never getting called , therefore $scope.phones
undefined. getting error `cannot read length of undefined'.
Comments
Post a Comment