javascript - Three condition button based on Angular -
i new angular feeling lost in of it's documentation.
problem: trying create button has 3 phases:
add user - remove request - remove user
- so if want add user click on add button, sends ajax request server , if successful button should turn pending button.
in pending state if click on again, request deleted , again turn add button.
the third phase if user has accepted request, seeing remove user button when click on
again see add button if click pending button , forth.
so familiar button if you've been using social networks.
when page loaded user see users , buttons each user based on it's current condition (so server handling part). part angular should handle ajax calls , changing of button per user connection request.
what need: have done ajax part sending request. can't manage handle part angular needs change button it's new state (for specific user on list, meaning list has more 1 user can send connection add/pending/delete requests.) have tried different solutions failed till now.
some of messy failure code have left unfinished:
angular controller:
foundationapp.controller('connectionbuttonctrl', function ($scope, $http) { $scope.adduser = function(id) { $http({ method : 'get', url : '/api/connections/add/'+id, datatype: "html", }) .success(function() { $scope.activeid; $scope.activeid = id; $scope.isadd = function(id){ return $scope.activeid === id; }; }) }; $scope.removerequest = function(id) { $http({ method : 'get', url : '/api/connections/removerequest/'+id, datatype: "html", }) .success(function() { }) }; });
laravel blade view:
<span ng-controller="connectionbuttonctrl" > <a class="label radius fi-plus" ng-show="!isremove(1)" ng-click="adduser(1)"></a> <a class="label radius fi-clock info" ng-show="isremove(1)" ng-click="removerequest(1)"></a> <a class="label radius fi-x alert" ng-show="!isadd(1)" ng-click="removeuser(1)"></a> </span>
if understand correctly, use $index or user.id. assuming buttons on same line user. if that's true using ng-repeat.
for example:
<div ng-repeat="user in users"> <a class="label radius fi-plus" ng-show="!isremove(user.id)" ng-click="adduser(user.id)"></a> <a class="label radius fi-clock info" ng-show="isremove(user.id)" ng-click="removerequest(user.id)"></a> <a class="label radius fi-x alert" ng-show="!isadd(user.id)" ng-click="removeuser(user.id)"></a> <div> user information {{user.name}} </div> </div>
then can pass id of user ajax request. can use $index (as parameter in code instead index of user in array).
Comments
Post a Comment