添加时为什么数据在列表中显示两次?

时间:2015-04-27 10:45:16

标签: javascript angularjs angularjs-directive angularjs-scope ionic-framework

我正在尝试制作简单的演示,其中我在数据库中添加了项目,但我面临一个问题。当我添加数据时,它会在视图中显示多个时间。 按照给定的步骤

  1. 点击右下方按钮。显示弹出屏幕。输入字段上的文字。然后按添加按钮。添加到数据库中。显示列表中的文字。没关系。但如果再添加则显示上一个文本以及当前文本。它显示重复的项目。但它不存在于数据库中。
  2. 换句话说。按下底部按钮。输入" A"在它显示" A" row.Secondly add" B"文本。它显示" A"," A"," B"。但是" A"和" B"出现在数据库中。为什么" A"显示更多?
  3. 这是我的代码 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);
            }
    
    
    })
    

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 = [];