2016-08-07 8 views
0

を見る:

 $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) { 
        $cordovaPreferences.show().success(function(value) { 
         //alert("Success: " + value); 
        }).error(function(error) { 
         // alert("Error: " + error); 
        }) 
       } else { 
        console.log('You are not sure'); 
       } 
      }); 
     } 
    }, true); 

それは、この入力を見る:

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

しかし、私は私の見解を開いたとき、私はこのエラーがあります:

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

どうすればこのkを回避できますかエラーインディー?この値 'Erreur ...'を見て、confirmPopupを呼び出すより良い解決策がありますか?ありがとうございます。

+0

このコードから 'infos.location'はどのように' .formatted_address'プロパティを取得しますか? – Claies

+0

これは '' ion-google-place "'ディレクティブから来ます。それは入力です、あなたはそれをクリックして、あなたはあなたのジオロケーション/フォーマットアドレスと他のものを持っています:https://github.com/israelidanny/ion-google-place –

+0

あなたは本当にこのようにすることはできません、と私は思います。そのコードには約束があり、約束が返された後に 'ng-model'が変更されています。あなたの '$ watch'は、入力がユーザによって変更されるたびに少なくとも2回、あるいはそれ以上になることがあります。あなたは 'if(newValue.formatted_address){'のようなガード・ステートメントを置くことができますが、これはパフォーマンスの問題を引き起こす可能性があります。 – Claies

答えて

1

あなたの'infos.location'はオブジェクトではありません。初期化していないか、文字列として初期化していない可能性があります。

infosオブジェクトまたはコントローラ全体をどのように初期化したかを確認したいと思います。

おそらく、infosを初期化することでこれを修正できます。

infos: { 
    location: {}, 
    ... 
} 

あるいは、イオングーグル-場所infos.locationを初期化する場合infos.location場合、あなただけ(すなわち。関数内newValue)を確認することができ、その状況(ない)undefined、ちょうど無視(スキップ)です。

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

オブジェクトを初期化しましたが、位置キーは初期化していません...あなたのおかげで修正されました! –

0

これは私がそれを考え出した方法です:私のコントローラで、後に続いて

$scope.infos = { 
     nom: '', 
     tel: '', 
     location: {}, 
     lat: '', 
     lng: '' 
    }; 

:私は私の見解で入力されたとき

 $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) { 
        $cordovaPreferences.show().success(function(value) { 
         //alert("Success: " + value); 
        }).error(function(error) { 
         // alert("Error: " + error); 
        }) 
       } else { 
        console.log('You are not sure'); 
       } 
      }); 
     } 
    }, true); 

infos.locationが初期化されませんでした。今それは動作します。皆さん、ありがとうございました !

関連する問題