单击对象数组中的值将返回另一个键AngularJS的值

时间:2017-10-11 15:40:05

标签: javascript angularjs arrays object

我在列表中迭代了一个对象数组。如果我点击名称,它应该提醒我名字对应的城市的价值。

<div ng-app='myApp'>
    <div ng-controller="AppController">
      <div ng-click="getCity()" ng-repeat="step in steps">
        <p> {{step.name}}</p> 
    </div>
</div>

var myApp = angular.module('myApp', []);
myApp.controller('AppController', function ($scope) {
    $scope.steps = [
        {name: "ABC", status: true, city: "Boston"},
        {name: "DEF", status: true, city: "New York"},
        {name: "GHI", status: true, city: "LA"}
];

        $scope.getCity = function(a) {

    }
});

Detailed code here.

- &GT;所以我点击&#34; ABC&#34;和#34;波士顿&#34;应该向我展示。

感谢帮助。感谢。

1 个答案:

答案 0 :(得分:2)

您可以将值传递给getCity函数:

&#13;
&#13;
var myApp = angular.module('myApp', []);
myApp.controller('AppController', function($scope) {
  $scope.steps = [{
      name: "ABC",
      status: true,
      city: "Boston"
    },
    {
      name: "DEF",
      status: true,
      city: "New York"
    },
    {
      name: "GHI",
      status: true,
      city: "LA"
    }
  ];

  $scope.getCity = function(step) {
    alert(step.city)
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp'>
  <div ng-controller="AppController">
    <div ng-click="getCity(step)" ng-repeat="step in steps">
      <p> {{step.name}}</p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;