从函数返回时,空数组显示非空

时间:2016-10-13 08:55:12

标签: html angularjs

我试图将一些项目推入阵列但为了简单起见,我使用以下代码作为示例。 filterObj是一个空对象,但当推送到数组filterArr时,filterArr包含[{}]。我试图放入if条件:

if(filterObj != {}) {
    $scope.filterArr.push(filterObj);
}

上述if条件不起作用,因为输出结果仍然是[{}]

代码:

<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
function MyCtrl($scope) {
    $scope.lines = [];

    $scope.filterArr = [];
    var filterObj = new Object();
    filterObj = {};
    $scope.filterArr.push(filterObj);

    $scope.addLine = function () {
        $scope.lines.push($scope.lines.length);
    };
}
</script>
</head>
<body>
  <div ng-app ng-controller="MyCtrl">
    <button ng-click="addLine()">Run</button>
    <div ng-repeat="line in lines">
        FilterArr : {{filterArr}}.
    </div>
</div>
</body>
</html>

我希望看到的是,在输出filterArr时,它应显示[]而不是[{}] 谢谢你的帮助!

0 个答案:

没有答案
相关问题