Hello everyone, I just started to learn AngularJS, so do not judge strictly. I have this question: I get this JSON from the API:
{ "isCar": true // false } There is a service that knocks on the IPA and takes what has come:
function checkIfCar(carId) { $http.get(env.api + 'cars/iscar/' + carId).success(response => { return response.data.isCar; //так я хочу достать true или false }).error(function (data) { toastr.error('Something went wrong and we can\'t check:' + data); }); } There is a directive from which I call this method:
function checkIfCar() { var isCar = carService.checkIfCar(carId); if (isCar === false) { toastr.warning('This is not a car!'); } } When I try to get it, everything comes good with the IPA, but Angulyar swears
Cannot read property 'isCar' of undefined
Tell me, please, how to extract the value that came, I will be very grateful!
$http.getis an asynchronous method and how to work with promises? - MrFylypenkosuccessremove thereturnand add the codeif (response.data.isCar === false) { toastr.warning('This is not a car!'); }if (response.data.isCar === false) { toastr.warning('This is not a car!'); }- MrFylypenkoresponse.data.isCar,Cannot read property 'isCar' of undefined- better start with a printout of the response in the console and a careful examination of its structure. and only then promises, nuggets, sneakers - nörbörnën