Pablo DelaNoche Pablo DelaNoche - 2 months ago 7x
AngularJS Question

angular $watch empty value error

I have this in my controller :

$scope.$watch('infos.location', function(newValue, oldValue) {
if (newValue.formatted_address == 'Erreur ...') {
var confirmPopup = $ionicPopup.confirm({
title: 'Geolocalisation',
template: 'Voulez-vous activer la géolocalisation ?'
confirmPopup.then(function(res) {
if (res) {
$ {
//alert("Success: " + value);
}).error(function(error) {
// alert("Error: " + error);
} else {
console.log('You are not sure');
}, true);

It watch this input :

<label class="item item-input">*
<ion-google-place placeholder="Veuillez saisir l'adresse du bien" ng-model="infos.location" current-location="true"/>

But when i open my view I have this error :

Error: undefined is not an object (evaluating 'newValue.formatted_address')

How can I avoid this kind of error ? Is there a better solution to Watch this value 'Erreur ...' and start calling confirmPopup ? Thank you.


Your 'infos.location' isn't object, probably you haven't initialize it or initialize it as string.

I would like to see how you initialized your infos object or whole controller.

Probably, you could fix this by initializing infos.

infos: {
  location: {},

Or, if ion-google-place initialize infos.location, you can just check if infos.location (ie. newValue inside function) is (not) undefined, and just ignore (skip) that situation.

 $scope.$watch('infos.location', function(newValue, oldValue) {
        if (newValue && newValue.formatted_address == 'Erreur ...') {
    }, true);