2

I have 3 blocks that are showed based on a boolean.

<button ng-if="user.friendship == null" ng-click="requestFriendship(user.user.id)" class="button button-outline button-calm">
    Invite as friend
</button>
<button ng-if="user.friendship == false" ng-click="removeFriendship(user.user.id)" class="button button-calm">
    Friend request sent
</button>
<button ng-if="user.friendship == true" ng-click="removeFriendship(user.user.id)" class="button button-calm">
    Unfriend
</button>

Well at first i don't know if it is the best solution to structure it that way, so do not hesitate to correct me here if i'm wrong.

then i have my function :

$scope.requestFriendship = function(id) {

    $http.post(domain+'/api/friendship/request/'+id+'?access_token='+access_token.key).then(function(response){
        // If success change button
    }, function(error) {
        console.log(error);
    });

}

$scope.requestFriendship = function(id) {

    // function

}

So based on the result (my api is returning success or failed, so if success), I need to Hide the previous button and change it to its new state.

So how can i hide and show buttons based on the answer of the API.

5
  • 2
    There's no right or wrong answer. You could do it with 1, 2, or 3 buttons Commented May 21, 2015 at 8:45
  • Sure, so in that case how could i show and hide buttons if response is success? Actually i wonder if i should use ng-show instead of ng-if. Commented May 21, 2015 at 8:48
  • You would set $scope.user.friendship to the correct value. Commented May 21, 2015 at 8:51
  • 1
    ngShow is very similar to ngIf, the difference being that the element will be added/removed from the dom with ngIf and just shown/hidden for ngShow Commented May 21, 2015 at 8:53
  • Okay that is perfectly working. I gotta think AngularJS and stop thinking JQuery haha, notion of scope if pretty new to me. If you want to set it as an answer i'll be glad to validate it. Commented May 21, 2015 at 8:57

1 Answer 1

1

You would set $scope.user.friendship to the correct value to show and hide the relevant button:

This will show the unfriend button:

$scope.user.friendship = true;

This will show the Friend request sent button:

$scope.user.friendship = false;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.