我有以下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?
};
};
})();
答案 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;
};