AngularJS:当多条消息更改消息的 ngModel 时不显示警报 [英] AngularJS: Alerts don't show up when multiple messages change ngModel for message

查看:26
本文介绍了AngularJS:当多条消息更改消息的 ngModel 时不显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我有一个通知服务,在加载页面和加载控制器时运行良好

  • 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

这是一个 plunker - http://plnkr.co/edit/YioiJXNkaET6T2mexjCq?p=预览

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

每当 $scope.message 发生变化时,我需要做什么来更新它?

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

推荐答案

您可以$watch 模型并在模型发生变化时显示警报.http://plnkr.co/edit/fJuP9LWH4MNVV1cQs3ED?p=preview

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

这篇关于AngularJS:当多条消息更改消息的 ngModel 时不显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆