2

I want to make a clickable list in angular, after clicking on the item, it should fire a function in controller where I want to manipulate the item. The code is:

<h2>User List</h2>
    <ul ng-repeat="item in items">
        <li ng-repeat="(key, val) in item">
            <ul>
                 <li>
                      <div  ng-click="getUserChat(key)">
                          <a>{{key}}</a>
                      </div>
                 </li>
             </ul>
         </li>
     </ul>

in controller:

chatApp.controller('chatController', ['$scope', '$rootScope','$state', '$http' , function($scope, $rootScope, $state, $http) {
$scope.init = function(){
    $http.get("http://127.0.0.1:8000/user_list/").success(function(data) {
        $scope.items = data;
        console.log($scope.items);
    });
};
$scope.getUserChat = function(selectedFriend){
    $rootScope.selectedFriend = selectedFriend;
    console.log($rootScope.selectedFriend);
}
}]);

But the console.log is not showing anything, that is the associated function getUserChat is not called. It's also not showing any error in console.

1
  • Can you provide a jsfiddle that demonstrates your issue, since your approach is correct. There should be no issue, like seen in this fiddle: jsfiddle.net/cemozer/nPRRB/2 Commented Apr 3, 2014 at 13:04

1 Answer 1

2

Not sure why you're using (key,value) in your expression, you could just do this...

<li ng-repeat="item as item.name in item">

(assuming you want to display a property of the item called "name" in the template, you may want to display something else).

Then, you just pass the whole item to the function...

<div  ng-click="getUserChat(item)">

So that the function has access to everything.

Also, not sure whether ng-click works on a div, maybe that is why your function isn't being called.

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.