2016-08-30 83 views
0

我想在我的數據範圍中的數組對象中的某個元素上設置布爾屬性。在AngularJS應用中未定義的作用域變量

在下面給出的代碼中,當我嘗試設置任務[id] .deleted = true時,出現以下錯誤。

angular.js:12798 TypeError: Cannot set property 'deleted' of undefined 
at Scope.$scope.delete (main.js:54) 

我在哪裏出錯了?

我的整個代碼文件是:

angular.module('ngMaterialTaskListApp') 
.controller('MainCtrl', function ($scope, $mdDialog, TaskService) { 

    // Model from which View populates data 
    $scope.tasks = []; 
    console.log($scope.tasks); 

    $scope.showAddDialog = function (ev) { 
     $mdDialog.show({ 
      controller: DialogController, 
      templateUrl: '../views/add-dialog-template.html', 
      parent: angular.element(document.body), 
      targetEvent: ev, 
      clickOutsideToClose: true, 
      fullscreen: true, //Only for xs and sm screen sizes 
      locals: { //For DialogController, as tasks 
       tasks: $scope.tasks 
      } 
     }); 
    }; 
    /*----------- Function to delete items onClick of delete icon -----------*/ 
    $scope.delete = function (id) { 
     console.log($scope.tasks[id]); 
     console.log(id); 
     // console.log($scope.tasks[id].name); 
     $scope.tasks[id].deleted = true; 
    }; 

    /*----------- DialogController function -----------*/ 
    function DialogController($scope, $mdDialog, tasks) { 
     $scope.task = {}; 
     $scope.hide = function() { 
      $mdDialog.hide(); 
      //TODO Add a message as to what happened 
     }; 
     $scope.cancel = function() { 
      $mdDialog.cancel(); 
      //TODO Add a message as to what happened 
     }; 
     /*----------- Method show the add dialog -----------*/ 
     $scope.addData = function() { 
      if (null !== $scope.task.name && null !== $scope.task.description) { 
       /*----------- Using moment.js to parse date and time -----------*/ 
       $scope.task.date = moment($scope.task.date, '').format('DD MMM YYYY'); 
       $scope.task.time = moment($scope.task.time, '').format('h:mm a'); 
       $scope.task.done = false; // Every new task is pending! 
       $scope.task.deleted = false; // Every new task exists! 
       var GlobalID = Date.now(); 
       console.log(GlobalID); 
       $scope.task.id = GlobalID; 
       /*----------- Performing http POST -----------*/ 
       TaskService.postTask($scope.task); 
       /*----------- Pushing to tasks object in $scope of MainCtrl -----------*/ 
       // Have to update tasks again 
       tasks.push($scope.task); 
       $scope.hide(); 
       console.log(tasks); //DEBUGGING 
      } else { 
       //TODO ADD INVALID/NULL DATA WARNING 
      } 
     }; 
    }; 
    // DEPRECATED - USED FOR DATA WHEN SERVER NOT AVAILABLE 
    TaskService.getTasks().then(function (response) { 
     $scope.tasks = response.data.tasks; 
    }, function (error) { 
     console.log(error + "This"); 
    }); 
    //USING THIS TO GET DATA FROM SERVER 
    TaskService.getAllTasks().then(function (response) { 
     // console.log(response.data); 
     $scope.tasks = response.data; 
     console.log($scope.tasks); 
    }); 
}); 
+0

檢查是否'$範圍。任務[id]'存在是因爲它的'undefined' – gianlucatursi

+1

您正在使用該數組中的某個項目的ID,但是'$ scope.tasks [id] .deleted'將使用從零開始的索引來查找項目。例如,一個包含3個項目的數組將會是[0],[1]或[2] ..如果您傳遞的值爲4,那麼您將在3個元素的數組中搜索第5個元素。因此元素將是未定義的,並且不會有「已刪除」的屬性 – Jorrex

回答

0

如何是你的HTML?我的辦法是這樣的NG-重複一個按鈕內:

ng-click="delete(task.id)" 

嘗試把這樣的:

ng-click="delete($index)" 
相關問題