AngularJS: Alerts don't show up when multiple messages change ngModel for message

人盡茶涼 提交于 2019-12-07 22:07:17

问题


  • I have a notification service which works well for when page is loaded and Controller is loaded

  • But when I have different buttons calling different functions, they change message, but alerts don't show up

Here is a plunker for that - http://plnkr.co/edit/YioiJXNkaET6T2mexjCq?p=preview

What is that I need to do to update it whenever $scope.message changes?


回答1:


You could $watch the model and show the alert when it changes. http://plnkr.co/edit/fJuP9LWH4MNVV1cQs3ED?p=preview

In linker function of your directive:

link: function(scope, element, attrs) {
  scope.$watch('ngModel', function() {
    element.show();
    $timeout(function(){
      //element.empty();
      element.hide();
    }, 5000);
  });
}


来源:https://stackoverflow.com/questions/16825761/angularjs-alerts-dont-show-up-when-multiple-messages-change-ngmodel-for-messag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!