javascript - AngularJS - Shared service object being deleted incorrectly -


when trigger deletequestion() second time 2 questions deleted. idea? let me know if need see more of code.

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);     }; }); 

edit: i'm thinking it's event propagation thing, since when have 5 q's deletes #2 , #3 when delete #2.

edit: fixed, see controller.js code.

it appears adding 'click' function #confirmationmodal #confirm button multiple times. first time $scope.deletequestion called, adds function. second time call it, adds again when clicked, function called twice.

a simple fix unbind 'click' event before adding again. this: $('#confirmationmodal #confirm').off('click');

the better solution here not use jquery @ these event bindings. using simple angular modal directive (like 1 provided in angular-ui library, instance) correct way this. can have ng-click on button , never have problem.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -