我想知道如何清空数组中的元素列表,以仅检索当前元素。 让我解释一下,在我使用的搜索表单中:
- 用于在数组中添加 id 的 addObject 函数。
- 和一个检索此 ID 的 getObject 函数。
它第一次毫无问题地工作,并向我显示了我想要的数据。在我的数组中,例如:[1]
但是当我返回搜索表单并再次开始操作时,这就是我的数组中的内容:[1,2]
这是我的代码:
我的 Controller :
.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>
我会尽快回复,谢谢你的回答:)
最佳答案
正如 CrazyMac 所说,你必须初始化数组
var addObject = function(newObj) {
element = [];
element.push(newObj);
}
关于javascript - 如何清空表中的项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43367344/