當我第二次觸發被刪除。任何想法?如果您需要查看更多我的代碼,請告訴我。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