我正在尝试制作简单的演示,其中我在数据库中添加了项目,但我面临一个问题。当我添加数据时,它会在视图中显示多个时间。 按照给定的步骤
这是我的代码 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);
}
})
答案 0 :(得分:0)
只需替换它:
window.onload = function() {
var doSomethingAJAX = function (el, url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function () {
var data = JSON.parse(xhr.responseText);
var tasks = data.data;
var aTags = document.getElementsByTagName("A");
for(var h = 0; h < tasks.length; h++){
var tabContainer = document.getElementById("tab-container");
var divElement = document.createElement("div");
divElement.className = 'tab-content';
tabContainer.appendChild(divElement);
var hOneElement = document.createElement("h1");
hOneElement.className = 'tab';
hOneElement.setAttribute('title', 'task' + h);
hOneElement.innerHTML = tasks[h].name;
divElement.appendChild(hOneElement);
var formElement = document.createElement("form");
formElement.className = 'taskForm';
formElement.setAttribute('action', 'CompleteTask');
formElement.setAttribute('method', 'post');
divElement.appendChild(formElement);
var taskId = tasks[h].id
getFormProperties(taskId, h);
}
};
xhr.onerror = function () { alert("error") };
xhr.send();
};
你必须清空$ scope.items = [];