Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the ng-click button was clicked already

I am toggling my div on ng-click using isVisible. The problem I am having is that every time I click the button, it runs $scope.Objectlist.push(data);. I want to only push it on the first click but at the same time I want it to push if its fetching a different object. I have a button beside every row and each row has a different id which is getting passed as the parameter for the button function.

HTML:

<tr ng-repeat="object in objectlist">
    <td>{{object.id}}</td>
    <td><button ng-click="pushData(object.id)">View</button></td>
</tr>

JS:

$scope.objectlist = [];
$scope.isVisible = false;
$scope.pushData= function(id) {
    $http.get("some variables being passed on here").success(function(data, status, headers, config){
            $scope.objectlist.push(data);
    }).error(function(data, status, headers, config){
        alert("Error");
    });
    $scope.isVisible = ! $scope.isVisible;
};

I have multiple different objects some are empty and some are not so this function cannot just check for the length of the list

like image 924
user4756836 Avatar asked Dec 05 '25 05:12

user4756836


1 Answers

What about storing the visibility per object id (I didn't test it) :

HTML

<tr ng-repeat="object in objectlist">
    <td>{{object.id}}</td>
    <td><button ng-click="pushData(object.id)">View</button></td>
</tr>

JS

$scope.objectlist = [];
$scope.isVisible = false;
var store = {}; // Store visibility (boolean) per object id

$scope.pushData= function(id) {
    // If not stored yet
    if (!store[id]) {
        $http.get("some variables being passed on here").success(function(data, status, headers, config){
            $scope.objectlist.push(data);
            store[id] = true; // Store it and set true for the visibility
        }).error(function(data, status, headers, config){
            alert("Error");
        });
    }
    $scope.isVisible = !store[id]; // Set the visibility depending on if the object is already in the store or not
};

I'm not sure about the $scope.isVisible = !store[id]; since I don't really know the interaction it has within the view. But something similar to that could do the trick

like image 182
lkartono Avatar answered Dec 07 '25 19:12

lkartono