2017-10-11 65 views
0

我有一个迭代列表中的对象数组。如果我点击这个名字,它应该告诉我这个名字对应的城市的价值。单击一个对象数组中的值返回另一个键的值AngularJS

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

- >所以我点击 “ABC” 和 “波士顿” 应该给我看了。

欣赏帮助。谢谢。

+0

什么是不工作? – UncleDave

+0

那么返回城市价值的功能是什么? – a2441918

+0

我已经回答了,但将来你应该真的告诉我们你试过了什么,什么都不行。如果你没有尝试过,那么你可以在AngularJS文档中轻松找到答案。 – UncleDave

回答

2

您可以将价值传递给您的getCity功能:

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) 
 
    } 
 
});
<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>

相关问题