0

I have the following HTML:

<div ng-controller="PageController as page">
    <li ng-repeat="user in page.user_data.users">
     {{user.name}}
     <button ng-model="user.followed" ng-click="page.toggleFollow(user.id)">{{user.follow_text}}</button>
    </li>
</div>

How do I access or change the value of user.followed inside the function toggleFollow?

(function(){
    angular.module('app')
    .controller('PageController', ['$scope', UserService, PageController]);

    function PageController($scope, UserService){

        var me = this;
        me.user_data = [];           

        me.getUsers = function(){
           UserService.getUsers().then(function(data){
             me.user_data = data;
           });
        };

        me.toggleFollow = function(user_id){
         //how to get or modify the value of user.followed from here?

        };

    };
})();
Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
  • Where is your user or your users inside of your controller? And in Angular, there isn't a need to reference this, since you should actually be referencing your injected $scope – David L Nov 13 '14 at 02:43
  • I'm getting the users data via a service, sec I'll update the code – Wern Ancheta Nov 13 '14 at 02:44
  • Is that data ever injected into your controller? – David L Nov 13 '14 at 02:46
  • Instead of passing `user.id` to `toggleFollow` function, just pass `user` itself, and inside `toggleFollow` you can directly access and change `user.followed` – Lokesh Nov 13 '14 at 02:50

1 Answers1

1

Instead of passing in just the id of the user you follow, pass in the entire user object.

<button ng-model="user.followed" ng-click="page.toggleFollow(user)">{{user.follow_text}}</button>

me.toggleFollow = function(user){
    var followed = user.followed;
};
David L
  • 32,885
  • 8
  • 62
  • 93