AngularJS应用中未定义的作用域变量
Undefined scope variable in AngularJS app
我试图在我的数组对象中的一个元素上设置一个布尔属性,我在我的范围内。
在下面给出的代码中,当我尝试设置tasks[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);
});
});
你的html怎么样?我打赌在ng-repeat:
的按钮内是这样的ng-click="delete(task.id)"
试着这样写:
ng-click="delete($index)"
相关文章:
- 当我更新另一个作用域变量时,作用域变量会自动更新
- 如何在隔离作用域指令中访问此作用域变量
- 正在向构造函数添加作用域变量
- 如何在angularjs中将多个作用域变量传递到自定义指令中
- 作用域变量未从状态父控制器继承到子控制器
- 插槽:访问作用域变量
- 在另一个作用域变量中使用AngularJS作用域变量
- 在Angular.js中通过检查作用域变量进行过滤
- 如何更新作为属性传入的作用域变量
- Node.js中的垃圾收集作用域-变量将保持设置状态多长时间
- 从$http.get调用更新Angular作用域变量
- AngularJS ui路由器:访问子作用域变量
- 访问AngularJS中函数中的作用域变量
- 为什么Angular 1.5双向绑定在将作用域变量传递给组件绑定时失败
- 修改作用域变量后,未更新作用域绑定
- 控制器作用域变量在刷新之前未加载
- 将Angular作用域变量的字符串描述传递给指令
- 将内部作用域变量绑定到外部“模板”
- 无法在 $.post 回调中设置作用域变量
- 如何从在 JS 中用作参数的匿名函数中分配外部作用域变量