首页 > 解决方案 > 从一个页面导航到另一个页面时如何显示消息

问题描述

在成功消息上,我想从上传的页面导航到客户页面并将我的警报突出显示为 success ,但我的警报没有打开。需要解决方案

上传.js

if (status == 200){
    $state.go('customer', {"id": $scope.customer});    
    $rootScope.$emit('custSucess');
}

客户.js

$rootScope.$on('custSucess',function(event){
         $scope.message = {
                content: [{
                   title: '',
                   msg:'hi'
                }],
                   type: 'success'
                };
});

标签: angularjsangular-ui-router

解决方案


所以我最终做的是创建一个服务来处理我的警报。这是服务代码:

app.factory('AlertService', function () {
  var success = {},
      error = {},
      alert = false;
  return {
    getSuccess: function () {
      return success;
    },
    setSuccess: function (value) {
      success = value;
      alert = true;
    },
    getError: function () {
      return error;
    },
    setError: function (value) {
      error = value;
      alert = true;
    },
    reset: function () {
      success = {};
      error = {};
      alert = false;
    },
    hasAlert: function () {
      return alert;
    }
  }
});

//而且我只是在需要时设置它:

AlertService.setSuccess({ show: true, msg: name + ' has been updated successfully.' });

//并在显示它的页面上检查它:

if (AlertService.hasAlert()) {
  $scope.success = AlertService.getSuccess();
  AlertService.reset();
}`enter code here`

推荐阅读