Rajesh Vishnani Rajesh Vishnani - 7 days ago 6
Javascript Question

2 ng-clicks on 2 different divs in ng-repeat does not work

First of all, sorry for the horrible title. I can explain well here.

I have 2 divs in ng-repeat and i want to call 2 different functions on 2 different divs, but for some reason only second div's ng-click works, please see the code below.



var app = angular.module("myApp", []);
app.controller("ContactListController", getUserContacts);

function getUserContacts($scope) {
$scope.contacts = [{
'name': 'abc',
'phone': '123',
'call_rate': 50,
'exchange_currency_name': 67
}, {
'name': 'xyz',
'phone': '345',
'call_rate': 20,
'exchange_currency_name': 67
}]

$scope.setcontact = function(x) {
alert("setContact");
}
$scope.DialCallfromHome = function(x) {
alert("dial");
}
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ons-list ng-app="myApp" ng-controller="ContactListController">
<input type="search" ng-model="search" class="search-input" style="width:95%; margin: 6px auto 6px auto;" placeholder="Search">
<div ng-show="loading" id="loadingDiv">
<img src="images/loader-logo.png" alt="">
<div id="loader"></div>
</div>
<ons-list-item modifier="chevron" class="list-item-container" ng-repeat="x in contacts| filter:search">
<div class="list-item-left" ng-click="DialCallfromHome(x);">
<div class="initial">
<img src="images/call-icon.png" style="width: 40px; margin: 5px;"></img>
</div>
</div>
<div class="list-item-right" ng-click="setcontact(x);">
<div class="list-item-content">
<div class="name">{{x.name}}
<br />
<span class="number">{{x.phone}}</span>
<span class="lucent">Call for {{x.call_rate}}&cent;/min</span>
</div>
<span class="desc">$1 = {{x.exchange_rate}} {{x.exchange_currency_name}}</span>
</div>
</div>
</ons-list-item>
<br>
<div ng-show="contacts.length == 0" style="width:95%; margin: 6px auto 6px auto;">No contacts added yet.</div>
</ons-list>




Answer

Here is working example

var app = angular.module("myApp", []);
app.controller("ContactListController", getUserContacts);

function getUserContacts($scope) {

  $scope.contacts = [{
    'name': 'abc',
    'phone': '123',
    'call_rate': 50,
    'exchange_currency_name': 67
  }, {
    'name': 'xyz',
    'phone': '345',
    'call_rate': 20,
    'exchange_currency_name': 67
  }]

  $scope.DialCallfromHome = function(n) {
    alert("dial");
  }

  $scope.setcontact = function(n) {
    alert("setcontact");
  }
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ons-list ng-app="myApp" ng-controller="ContactListController">
  <input type="search" ng-model="search" class="search-input" style="width:95%; margin: 6px auto 6px auto;" placeholder="Search">
  <div ng-show="loading" id="loadingDiv">
    <img src="images/loader-logo.png" alt="">
    <div id="loader"></div>
  </div>
  <ons-list-item modifier="chevron" class="list-item-container" ng-repeat="x in contacts| filter:search">
    <div class="list-item-left" ng-click="DialCallfromHome(x);">
      <div class="initial">
        <img src="http://www.freeiconspng.com/uploads/phone-call-icon-16.png" style="width: 40px; margin: 5px;"></img>
      </div>
    </div>
    <div class="list-item-right" ng-click="setcontact(x);">
      <div class="list-item-content">
        <div class="name">{{x.name}}
          <br />
          <span class="number">{{x.phone}}</span>
          <span class="lucent">Call for {{x.call_rate}}&cent;/min</span>
        </div>
        <span class="desc">$1 = {{x.exchange_rate}} {{x.exchange_currency_name}}</span>
      </div>
    </div>
  </ons-list-item>
  <br>
  <div ng-show="contacts.length == 0" style="width:95%; margin: 6px auto 6px auto;">No contacts added yet.</div>
</ons-list>