javascript - If array size is more than 0 then only push data -
i toggling div
on ng-click
using isvisible
. problem having everytime click button, runs $scope.objectlist.push(data);
. want push on first click or push if array size less or equal 0.
$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 tried using if(objectlist.length <= 0)
returned null array meaning nothing got pushed.
i don't think want use counter or boolean variable this. imagine set counter 1 or boolean variable "true" , other function you'll add later removes elements array. have empty array requires pushing, counter says it's not 0 , boolean variable says pushed array. wrong think should check array length directly this.
$scope.objectlist = []; $scope.isvisible = false; $scope.pushdata= function(id) { $http.get("some variables being passed on here").success(function(data, status, headers, config) { if ($scope.objectlist.length === 0) { $scope.objectlist.push(data); /* push if array empty */ } }).error(function(data, status, headers, config){ alert("error"); }); $scope.isvisible = ! $scope.isvisible; };
Comments
Post a Comment