2
I have a mobile app with the following problem: I have a list of users who each have a "follow" button, which when clicked will have to change to not follow.
Return of PHP true
or false
to know whether the user currently logged in is already following or not the user. What happens is that when I click it inserts the follower well, but when I refresh the page I go to by "don’t follow", but it re-enters the follower instead of removing it. I don’t know what the problem is.
Controller
.controller('ListaSeguidoresUser', function($scope, $http, sessionService) {
$http.get("https://www.sabeonde.pt/api/api_seguidores_user.php?user_slug="+sessionService.get('user_slug')+"&user_id="+sessionService.get('user_id')).success(function (data) {
angular.forEach(data, function(c) {
$scope.seguidores_user = data;
var hasLiked = c.hasLiked;
$scope.seguir= function (id){
if (!hasLiked) {
hasLiked = false;
c.seguir_user = "Não Seguir";
c.botao_seguir = "seguir_user_click";
$http.get("https://www.sabeonde.pt/api/api_seguir_user.php?follower="+sessionService.get('user_id')+"&followed="+id).success(function (data) {
$scope.seguir_user = data;
});
} else {
hasLiked = true;
c.seguir_user = "Seguir";
c.botao_seguir = "seguir_user_class";
$http.get("https://www.sabeonde.pt/api/api_remover_seguir_user.php?follower="+sessionService.get('user_id')+"&followed="+id).success(function (data) {
$scope.nao_seguir_user = data;
});
}
}
});
});
})
View
<div ng-controller="ListaSeguidoresUser">
<div class="row" ng-repeat="seguidores in seguidores_user">
<div class="col">
<div class="list">
<a style="border-top-right-radius: 10px; border-top-left-radius:10px; border:none;" class="item item-thumbnail-left" href="#">
<img style="border-radius: 10px;" src="{{seguidores.user_foto}}">
<span style="font-weight:700; font-size:14px; color: black;">{{seguidores.nome}}</span>
<p>Seguidores {{seguidores.seguidores}}</p>
<p>Opiniões {{seguidores.opinioes}}</p>
</a>
<div style="background-color: white; border-bottom-right-radius: 10px; margin:0px -1px 0px -1px; border-bottom-left-radius:10px; height: 45px;">
<div style="padding:5px 10px 0px 10px;">
<div ng-init="seguir_user=seguidores.seguir_user" ng-click="seguir({{seguidores.id}})" class="seguir_user" ng-class="botao_seguir=seguidores.botao_seguir" style="margin:0px 0px 0px 0px;"><i class="fa fa-user-plus"></i> {{seguir_user}}</div>
</div>
</div>
</div>
</div>
</div>
</div>
Caesar, your question is very confusing. Please edit your question and explain yourself better.
– Filipe Moraes
I return from php true or false to know if the user who is currently logged in is following or not the user so when I click follow when I update the page I go to do not follow and he inves to remove it back to insert again should be the if that is wrong
– César Sousa
See your controller, you have all your code inside the foreach. With each cycle you set your "hasLiked" variable again, which will stick to the value of the last item of the "data" array, which should be why it doesn’t work.
– Filipe Moraes
I took it off but now when I click it does nothing
– César Sousa
What returns api_segui_user.php ? A "user object"?
– Filipe Moraes
does not return anything and an Insert only
– César Sousa