jquery - How to execute code synchronously in angular js -
i facing 1 issue service getting data service , binding ui, text box , drop down there passing model ui binding properly.
issue drop down working asynchronously (its guess). please check below code
vendorservice.getvendordetailsforvendor().then(function(vendordetails) { if (vendordetails.id !== 0) { $scope.businesstype = vendordetails.businesstypeid; $scope.vendortype = vendordetails.vendortypeid; $scope.category = vendordetails.shopcategory; $scope.discountunit = vendordetails.discountunitid; $scope.selectedstate = vendordetails.stateid; $scope.selectedcityid = vendordetails.cityid; if ($scope.businesstype != null) { $scope.$apply(function() { (var = 0; < $scope.businesstypelist.data.length; i++) { if ($scope.businesstypelist.data[i].businesstypeid === $scope.businesstype) { $scope.businesstype = + 1; } } }); } if ($scope.vendortype != null) { $scope.$apply(function() { (var = 0; < $scope.vendortypelist.data.length; i++) { if ($scope.vendortypelist.data[i].vendortypeid === $scope.vendortype) { $scope.vendortype = + 1; } } }); } if ($scope.category != null) { console.log($scope.category); $scope.$apply(function() { (var = 0; < $scope.categorylist.data.length; i++) { if ($scope.categorylist.data[i].shopname === $scope.category) { $scope.category = + 1; } } }); } if ($scope.discountunit != null) { $scope.$apply(function() { (var = 0; < $scope.discountunitlist.data.length; i++) { if ($scope.discountunitlist.data[i].discountunitid === $scope.discountunit) { $scope.discountunit = + 1; } } }); } if ($scope.selectedstate != null) { $scope.$apply(function() { (var = 0; < $scope.statelist.data.length; i++) { if ($scope.statelist.data[i].id === $scope.selectedstate) { $scope.selectedstate = + 1; } } }); } if ($scope.selectedcityid != null && $scope.selectedstate != undefined) { $scope.$apply(function() { vendorservice.getcity($scope.selectedstate).then(function(citylist) { $scope.citylist = {}; $scope.citylist.data = citylist; (var = 0; < $scope.citylist.data.length; i++) { if ($scope.citylist.data[i].id === $scope.selectedcityid) { $scope.selectedcity = $scope.selectedcityid; } } }, function() { alert("error while fetching server"); }); }); } $scope.$apply(function() { $scope.inputdata = vendordetails; }); $scope.loading = false; } }, function() { alert("error while fetching server"); });
i getting error in chrome console
error : cannot read property 'data' of undefined
you should check if there defined, not if it's different null
. this:
// returns true if value (object, array, number bigger 0, ...) // returns false if falsy value (0, '', undefined, null) if ($scope.category) { // ... code }
here cool link explains this.
Comments
Post a Comment