当我第二次触发被删除。任何想法?如果您需要查看更多我的代码,请告诉我。AngularJS - 共享服务对象被错误删除
controller.js
crtPromoCtrl.controller('surveyCtrl', ['$scope', 'surveySrv', function($scope, surveySrv)
{
$scope.questions = surveySrv.getQuestions();
$scope.editQuestion = function(index)
{
surveySrv.setEditQuestion(index);
};
$scope.deleteQuestion = function(index)
{
$(document).off('click', '#confirmationModal #confirm');
$('#confirmationModal').modal('show');
$(document).on('click', '#confirmationModal #confirm', function()
{
surveySrv.deleteQuestion(index);
$scope.$apply();
});
};
}]);
service.js
crtPromoSrv.service('surveySrv', function()
{
var questions = [];
var editQuestion;
this.getQuestions = function()
{
return questions;
};
this.addQuestion = function(question)
{
questions.push(question);
};
this.setEditQuestion = function(index)
{
editQuestion = questions[index];
};
this.getEditQuestion = function()
{
return editQuestion;
};
this.clearEditQuestion = function()
{
editQuestion = undefined;
};
this.deleteQuestion = function(index)
{
questions.splice(index, 1);
console.log(questions);
};
});
编辑:我想这是一个事件传播的事情,因为当我有5个Q的它删除#2和#3当我删除#2。
编辑:修正,请参阅controller.js代码。
永远(永远)不在控制器中进行DOM操作。这是规则#1在尝试构建模块化代码时试图使用Angular进行测试。控制器应该从后备阵列中移除项目。操纵DOM是指令的意图。 – Enzey
明白了,谢谢! – jstudios