Angular.js - 如何在ng-repeat中访问ng-model的值

时间:2014-11-13 02:41:06

标签: javascript angularjs

我有以下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>

如何在user.followed函数中访问或更改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?

        };

    };
})();

1 个答案:

答案 0 :(得分:1)

不是只传入您关注的用户的id,而是传入整个用户对象。

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

me.toggleFollow = function(user){
    var followed = user.followed;
};