JZ.
JZ.

Reputation: 21877

Angular way to bind model from ng-repeat and send to new function

  <li ng-repeat="result in apiResult">
    <span class="">{{result.id}}</span>
  </li>

    <form ng-submit="findone()">
      <input type="text" ng-model="searchTerm" value=""></span>
      <input class="btn-primary" type="submit" value="go">
    </form>

The above is my template.

What is the "angular way" to bind the ng-model searchTerm, to the value of an individual {{result.id}} when a user clicks on this <span>{{result.id}}</span>?

Controller:

var app = angular.module('campaign', []);

function campaignCtrl($scope, $http) {

  $scope.search = function() {
      $http({method: 'GET', url: "http://localhost:3002/voters.json?token=17975700jDLD5HQtiLbKjwaTkKmZK7zTQO8l5CEmktBzVEAtY&street_name="+$scope.searchTerm}).
        success(function(data, status, headers, config) { 
        $scope.apiResult = data.voters;
      }).
        error(function(data, status, headers, config) {
      });
  };

  $scope.init = function(id) {
      $http({method: 'GET', url: "http://localhost:3002/voters.json?token=17975700jDLD5HQtiLbKjwaTkKmZK7zTQO8l5CEmktBzVEAtY&street_name="+id}).
        success(function(data, status, headers, config) { 
        $scope.apiResult = data.voters;
        console.log(data.voters)
      }).
        error(function(data, status, headers, config) {
      }); 
  };

  $scope.findone = function(searchTerm) {
      $http({method: 'GET', url: "http://localhost:3002/voters.json?token=17975700jDLD5HQtiLbKjwaTkKmZK7zTQO8l5CEmktBzVEAtY&id="+searchTerm}).
        success(function(data, status, headers, config) { 
        console.log(data);
        $scope.apiResult = data.voters;
      }).
        error(function(data, status, headers, config) {
      }); 
  };

}

function indyCtrl($scope, $http) {
}

Upvotes: 0

Views: 405

Answers (1)

Chandermani
Chandermani

Reputation: 42669

Not exactly sure about the question. I think you are looking for something like this

<li ng-repeat="result in apiResult">
    <span class="" ng-click="searchTerm=result.id">{{result.id}}</span>
</li>

Upvotes: 2

Related Questions