Я пытаюсь обновить логическое свойство всех объектов в массиве с тем же user_id. Ниже я обрисовал структуру MVC и попытался представить код как можно более кратким.Найти свойство в массиве и получить индекс
Модель:
var posts = [{id:1, user_id:1, is_following:true},
{id:2, user_id:1, is_cool:true},
{id:2, user_id:2, is_cool:false}];
Вид:
<div class="list" ng-repeat="post in posts">
<button ng-click="unCool(post.user_id,$index)" ng-if="post.is_cool === true">
Cool
</button>
<button ng-click="makeCool(post.user_id,$index)" ng-if="post.is_cool === false" >
not Cool
</button>
<p>{{post.id}}</p>
</div>
Контроллер:
$scope.makeCool =function(userid, index){
//this is an ajax request for brevity i excluded the service
coolService.makeCool(user_id)
.success(function (data) {
$scope.posts[index].is_following = true;
//How to find other indexes with same user id and make them true too
}).
error(function(error, status) {
//do something
});
}
$scope.unCool =function(userid, index){
//this is an ajax request for brevity i excluded the service
coolService.unCool(user_id)
.success(function (data) {
$scope.posts[index].is_following = false;
//How to find other indexes with same user id and make them false too
}).
error(function(error, status) {
//do something
});
}
это создает избыточную интуицию промежуточный массив. Все можно обрабатывать с помощью одной карты и 'if' внутри –