如果数组大小大于0,则仅推送数据

时间:2015-06-18 23:05:58

标签: javascript arrays angularjs toggle

我使用divng-click isVisible上切换$scope.Objectlist.push(data);。我遇到的问题是,每次单击按钮时,它都会运行$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; }; 。我想只在第一次单击或仅在数组大小小于或等于0时按下它。

if(Objectlist.length <= 0)

我尝试使用function config($stateProvider, $urlRouterProvider, $ocLazyLoadProvider, IdleProvider, KeepaliveProvider, adalAuthenticationServiceProvider, $httpProvider) { // Configure Idle settings IdleProvider.idle(5); // in seconds IdleProvider.timeout(120); // in seconds $urlRouterProvider.otherwise("/dashboards/dashboard_1"); $ocLazyLoadProvider.config({ // Set to true if you want to see what and when is dynamically loaded debug: true }); $stateProvider .state('dashboards', { abstract: true, url: "/dashboards", templateUrl: "views/common/content.html", }) .state('dashboards.dashboard_1', { url: "/dashboard_1", templateUrl: "views/dashboard_1.html", resolve: { loadPlugin: function ($ocLazyLoad) { return $ocLazyLoad.load([ { serie: true, name: 'angular-flot', files: [ 'js/plugins/flot/jquery.flot.js', 'js/plugins/flot/jquery.flot.time.js', 'js/plugins/flot/jquery.flot.tooltip.min.js', 'js/plugins/flot/jquery.flot.spline.js', 'js/plugins/flot/jquery.flot.resize.js', 'js/plugins/flot/jquery.flot.pie.js', 'js/plugins/flot/curvedLines.js', 'js/plugins/flot/angular-flot.js', ] }, { name: 'angles', files: ['js/plugins/chartJs/angles.js', 'js/plugins/chartJs/Chart.min.js'] }, { name: 'angular-peity', files: ['js/plugins/peity/jquery.peity.min.js', 'js/plugins/peity/angular-peity.js'] } ]); } } }) ,但它为我的数组返回null意味着什么都没有被推送。

2 个答案:

答案 0 :(得分:2)

我认为你不想为此使用计数器或布尔变量。想象一下,你将counter设置为1或布尔变量设置为“true”,稍后你将添加的其他一些函数将从数组中删除所有元素。然后你有空数组,需要推送,但计数器说它不是0和布尔变量说某些东西已被推送到数组。这显然是错误的,所以我认为你应该像这样直接检查数组长度。

$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 only if array is empty */
        }
    }).error(function(data, status, headers, config){
        alert("Error");
    });
    $scope.isVisible = ! $scope.isVisible;
};

答案 1 :(得分:1)

这很简单。试试这个。

$scope.Objectlist = [];
$scope.isVisible = false;
$scope.clicks = 0;                     // Count the number of clicks
$scope.pushData= function(id) {
    $http.get("some variables being passed on here").success(function(data, status, headers, config){
        $scope.clicks += 1;
        if ($scope.clicks === 1 || $scope.Objectlist.length === 0) {  
            $scope.Objectlist.push(data);
        }
    }).error(function(data, status, headers, config){
        alert("Error");
    });
    $scope.isVisible = ! $scope.isVisible;
};

创建一个存储点击次数的变量clicks。创建一个条件,检查点击次数是否等于1,并且数组的长度是0。