0

I have an array of objects iterated over in a list. If I click on the name, it should alert me with the value of the city the name corresponds to.

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

--> So I click on "ABC" and "Boston" should be shown to me.

Appreciate the help. Thanks.

4
  • What isn't working? Commented Oct 11, 2017 at 15:47
  • So what should be the function to return the value of city? Commented Oct 11, 2017 at 15:48
  • I've answered, but in future you should really tell us what you've tried and what didn't work. If you've tried nothing then you can easily find the answer in the AngularJS docs. Commented Oct 11, 2017 at 15:52
  • Sorry about that. Thank you. Commented Oct 11, 2017 at 15:57

1 Answer 1

2

You can pass a value to your getCity function:

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.