单击

时间:2017-06-12 07:49:27

标签: angularjs

我试图根据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响应是:

{
  "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"
        }
      ]
    }
  }
}

2 个答案:

答案 0 :(得分:0)

假设您的$scope.currentItem是所选城市的id,例如kanpur,那么当API返回数据时,请检查此值是否与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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我唯一改变的是我将obj作为getdetails函数的参数传递。

$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>