如何清空表中的项目列表

时间:2017-04-12 10:44:08

标签: javascript jquery angularjs arrays

我想知道如何清空数组中的元素列表,只检索当前元素。 让我用搜索表单解释一下:

  • 一个addObject函数,用于在数组中添加id。
  • 以及检索此id的getObject函数。

第一次没有问题,并向我显示我想要的数据。在我的数组中,我有,例如:[1]

但是当我回到搜索表单并再次开始操作时,那就是我的数组中的内容:[1,2]

这是我的代码:

我的控制员:

.controller('listCtrl', function($scope, $stateParams, $state, appService) {
$scope.search = function () {
    appService.searchUser($scope.form).then(function(response){
        console.log(response);
        $scope.list = response;
    });
};
$scope.showInfo = function(currObj){
    appService.addObject(currObj);
    console.log(currObj);
    $state.go('infoJeune');
}
})
.controller('infoCtrl', function($scope, $stateParams, appService) {
$scope.$on('$ionicView.beforeEnter', function (event, viewData) {
    viewData.enableBack = true;
}); 
$scope.info = appService.getObject();
console.log($scope.info);
appService.userinfo($scope.info).then(function(response){   
    $scope.userinfo = response; 
    console.log($scope.userinfo);           
}); 
})

在我的服务中:

var addObject = function(newObj) {
    element.push(newObj);
};
var getObject = function(){
    return element;
};

和我的模板:



<form ng-controller="listCtrl" id="app-form5" class="list" ng-submit="search()">
  <label class="item item-input" id="app-search1" style="">
    <i class="icon ion-search placeholder-icon"></i>
    <input placeholder="Nom d'un jeune" type="search" ng-model="form.pnom">
  </label>
  <a class="button button-positive  button-block" ng-click="search()">Search</a>
  <ion-list id="menu-list1">
    <ion-item ng-repeat="user in list track by $index" ng-click="showInfo(user.id)">{{ user.fname }}</ion-item>
  </ion-list>
</form>
&#13;
&#13;
&#13;

我会尽快回复,谢谢你的回答:)

1 个答案:

答案 0 :(得分:2)

正如CrazyMac所说,你必须初始化数组

var addObject = function(newObj) {
    element = [];
    element.push(newObj);
}