重定向到状态并显示成功消息

重定向到状态并显示成功消息

问题描述:

在一个简单的CRUD应用程序中,我希望在成功更新后,它将通过更新成功"消息重定向回项目列表.您将如何实现1.3.x的角度?我最初的想法是做这样的事情,但是必须在URL中添加成功变量是很愚蠢的.

In a simple CRUD app I would expect after a successful update, it would redirect back to the list of items with an "update successful" message. How would you accomplish this is angular 1.3.x? My initial thoughts were to do something like this, but having to have a success variable in the URL is silly.

$state.go('app.list', {
  success: {
    show: true,
    msg: name + ' has been updated successfully.'
  }
});

有人对此有合适的解决方案吗?

Anyone have a decent solution for this?

所以我最终要做的是创建用于处理警报的服务.服务代码如下:

So what I ended up doing is creating a service for handling my alerts. Here is the service code:

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

并在将其显示为这样的页面上进行检查:

And check for it on the page that would display it like this:

if (AlertService.hasAlert()) {
  $scope.success = AlertService.getSuccess();
  AlertService.reset();
}