2016-11-22 6 views
1

Когда я пытаюсь добавить SweetAlert2 в мою кнопку удаления в угловом, это предотвращает изменение объема модели. Можно ли использовать их вместе?Как сохранить масштаб при использовании SweetAlert2 с угловым?

Shown here in this Plunker (убедитесь, что вы добавить пункт первый, а затем вы можете удалить в примере)

код с SweetAlert, что не работает (ничего не происходит, когда я подтверждаю):

function fieldToolsController($scope, ParticipantFactory) { 
    var model = this; 
    model.participant = ParticipantFactory; 
    model.participant.hasRoles = model.participant.roles.length > 0; 

    model.deleteSelectedRole = function() { 
    for (var i = 0; i < model.participant.roles.length; i++) { 
     if (model.participant.roles[i] === model.participant.selected) { 

     swal({ 
      title: 'Are you sure?', 
      text: "You won't be able to revert this!", 
      type: 'warning', 
      showCancelButton: true, 
      confirmButtonColor: '#3085d6', 
      cancelButtonColor: '#d33', 
      confirmButtonText: 'Yes, delete it!' 
     }).then(function() { 

      //============================= 
      //LOSES SCOPE HERE OR SOMETHING 
      //============================= 
      model.participant.roles.splice(i, 1); 
      model.participant.hasRoles = model.participant.roles.length > 0; 
      if (model.participant.hasRoles) { 
      model.participant.selected = model.participant.roles[0]; 
      } 
      return; 
      //============================= 
      //============================= 
      //============================= 

     }); 

     } 
    } 
    }; 
} 

Вот та же функция, которая отлично работает с нормальным яваскриптом предупреждением:

And the plunker

function fieldToolsController($scope, ParticipantFactory) { 
    var model = this; 
    model.participant = ParticipantFactory; 
    model.participant.hasRoles = model.participant.roles.length > 0; 

    model.deleteSelectedRole = function() { 
    for (var i = 0; i < model.participant.roles.length; i++) { 
     if (model.participant.roles[i] === model.participant.selected) { 

     var c = confirm("Are you sure?"); 
     if(c){ 
      model.participant.roles.splice(i, 1); 
      model.participant.hasRoles = model.participant.roles.length > 0; 
      if (model.participant.hasRoles) { 
      model.participant.selected = model.participant.roles[0]; 
      } 
      return; 

     } 

     } 
    } 
    }; 
} 

ответ

0

Поскольку функция выполняется позже (это обещание), вам может потребоваться указать переменную model в качестве введенного параметра.

В вашей .then функции, попробуйте инъекционного переменную model в нее, как это:

.then(function(model) { 
    console.log(model) 
}); 
+0

Да - я определенно пытался что. Я надеялся, что это будет так просто. Дает эту ошибку: 'app.js: 26 Неподготовлено (в обещании) TypeError: Невозможно прочитать свойства" ролей "неопределенного' – RichC