2017-06-12 7 views
0

私は都市の緯度と経度をIDで求めようとしています。たとえば、kanpurをクリックすると、APIから返される緯度が表示されます。しかし、私がしたのは、APIから戻ってきた最後の都市だけの緯度を返すことです。をクリックするとリストの項目が表示されます

私app.jsのコードは次のとおりです。

.controller('AppCtrl', function($scope, 
     $http,$ionicModal,$cordovaGeolocation, $ionicLoading, $ionicPlatform) { 
     $scope.currentItem = 1; 
     $scope.latitude; 
    $scope.longitude; 

    $scope.ers = { 
     'agency_device_id' : '' 

    }; 

    $scope.submit = function(){ 
       var link = 'http://trendytoday.in/ers/api/DeviceAlarms'; 
       $http.post(link, {ers: $scope.ers},{headers: {'Content-Type': 'application/json'} }).then(function (res){ 
      // $scope.mssg = res.data.ers.resMessage; 
      // $scope.resp = res.data.ers.response; 

       $scope.arr = []; 
       angular.forEach(res.data.ers.data.alarms, function(value) { 
       $scope.arr.push(value); 
       //console.log(value.city) 
       latitude=value.lattitude; 
       longitude=value.longitude; 



      }) 
    }); 
    } 

    $scope.getdetails = function(){ 
    window.alert(latitude); 
}; 
}); 

とAPIの応答は次のとおりです。

APIが返したとき、あなたの $scope.currentItemが、その後、このよう kanpurとして選択した都市の idであると仮定すると、
{ 
    "ers": { 
    "resMessage": "1", 
    "response": "Success", 
    "data": { 
     "alarms": [ 
     { 
      "id": "1", 
      "alarm_id": "2", 
      "title": "Fire", 
      "description": "fire", 
      "type": "Fire", 
      "priority": "High", 
      "address": "Kanpur, Uttar Pradesh, India", 
      "city": "Kanpur", 
      "state": "UP", 
      "country": "India", 
      "zipcode": "123456", 
      "lattitude": "26.449923", 
      "longitude": "80.3318736" 
     }, 
     { 
      "id": "3", 
      "alarm_id": "4", 
      "title": "test-02", 
      "description": "test-02", 
      "type": "Medical", 
      "priority": "High", 
      "address": "Borivali West, Mumbai, Maharashtra, India", 
      "city": "Mumbai", 
      "state": "MH", 
      "country": "India", 
      "zipcode": "123456", 
      "lattitude": "19.2461644", 
      "longitude": "72.85090560000003" 
     } 
     ] 
    } 
    } 
} 
+0

agency_device_idは常に空の文字列 – Ursache

+0

あなたは 'forEach'で緯度と経度の範囲を設定している、それだけで[値]を最後に設定されますです。 – Sajal

+0

@サハル; yaa thats thats問題を解決するには –

答えて

0

この値がidに一致することを確認し、$scopeの変数を設定します。

私はあなたがそのアイデアを得ることを願っています。

function MyCtrl($scope) { 
 
    $scope.currentItem = 1; 
 
    $scope.latitude; 
 
    $scope.longitude; 
 

 
    $scope.submit = function() { 
 
    $scope.res = { 
 
     data: { 
 
     "ers": { 
 
      "resMessage": "1", 
 
      "response": "Success", 
 
      "data": { 
 
      "alarms": [{ 
 
       "id": "1", 
 
       "alarm_id": "2", 
 
       "title": "Fire", 
 
       "description": "fire", 
 
       "type": "Fire", 
 
       "priority": "High", 
 
       "address": "Kanpur, Uttar Pradesh, India", 
 
       "city": "Kanpur", 
 
       "state": "UP", 
 
       "country": "India", 
 
       "zipcode": "123456", 
 
       "lattitude": "26.449923", 
 
       "longitude": "80.3318736" 
 
       }, 
 
       { 
 
       "id": "3", 
 
       "alarm_id": "4", 
 
       "title": "test-02", 
 
       "description": "test-02", 
 
       "type": "Medical", 
 
       "priority": "High", 
 
       "address": "Borivali West, Mumbai, Maharashtra, India", 
 
       "city": "Mumbai", 
 
       "state": "MH", 
 
       "country": "India", 
 
       "zipcode": "123456", 
 
       "lattitude": "19.2461644", 
 
       "longitude": "72.85090560000003" 
 
       } 
 
      ] 
 
      } 
 
     } 
 
     } 
 
    } 
 

 
    $scope.arr = []; 
 
    angular.forEach($scope.res.data.ers.data.alarms, function(value) { 
 
     $scope.arr.push(value); 
 
     if ($scope.currentItem == value.id) { 
 
     $scope.latitude = value.lattitude; 
 
     $scope.longitude = value.longitude; 
 
     } 
 
    }); 
 
    } 
 
    $scope.submit(); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<body ng-app ng-controller="MyCtrl"> 
 

 
Long: {{longitude}} 
 

 
Lati: {{latitude}} 
 
</body>

0

私が変更さ唯一の事は、私はgetdetails関数へのパラメータとしてOBJを通過しています。

$scope.getdetails = function(obj){ 
    alert(obj.lattitude); 
} 

とhtmlで:

<div ng-repeat="obj in arr " ng-click="getdetails(obj)"> 
     <p><b>{{obj.city}}</b></p> 
    </div> 
関連する問題