I am trying to make simple demo in which I added item in database but I am facing one issue.When I am adding data it show multiple time in view . Follow the given steps
here is my code http://codepen.io/anon/pen/OVPgoP
app.controller('cntr',function($scope,$ionicPopup,sqlservice){
console.log("main controller");
sqlservice.setup();
// sqlservice.deleteTable();
$scope.input={};
$scope.items=[];
$scope.itemToEdit = 0;
$scope.iseditDone=false;
$scope.loadTask = function() {
sqlservice.getAllCases().then(function(data){
console.log(data);
for(var i=0;i<data.length;i++)
$scope.items.push(data[i]);
console.log($scope.items);
})
}
$scope.loadTask();
$scope.showPopup = function() {
$scope.item = {}
// An elaborate, custom popup
var myPopup = $ionicPopup.show({
template: '<input type="text" ng-model="item.CaseName" style="border: 1px solid red" autofocus>',
title: 'Enter Add Test case',
subTitle: 'Add Test case name',
scope: $scope,
buttons: [
{ text: 'Cancel' },
{
text: '<b>Add</b>',
type: 'button-positive',
onTap: function(e) {
if (!$scope.item.CaseName) {
//don't allow the user to close unless he enters wifi password
e.preventDefault();
} else {
return $scope.item;
}
}
},
]
});
myPopup.then(function(res) {
console.log('Tapped!', res);
if(typeof res!='undefined' && !$scope.iseditDone) {
// $scope.items.push(res);
console.log($scope.items)
sqlservice.insertData(res.CaseName);
$scope.loadTask();
}else if(typeof res!='undefined' && $scope.iseditDone){
$scope.iseditDone = false;
// $scope.items[$scope.itemToEdit] = res;
sqlservice.updatecaseName(res.CaseName,$scope.itemToEdit);
$scope.loadTask();
}
console.log($scope.items);
});
};
$scope.addTestCase=function(){
$scope.showPopup();
}
$scope.editRow=function(item,row){
$scope.iseditDone=true;
$scope.itemToEdit = row
// alert($scope.data.testcase)
$scope.showPopup();
$scope.item.CaseName= item.CaseName;
}
$scope.deleterow=function(item,row){
$scope.items.splice(row, 1);
}
})
just Replace This:
sqlservice.getAllCases().then(function(data) {
console.log(data);
$scope.items = [];
for (var i = 0; i < data.length; i++)
$scope.items.push(data[i]);
console.log($scope.items);
})
you have to empty the $scope.items = [];