使用UI-Router和AngularJs设置位置更改后的消息

Setting up a message after location change with UI-Router and AngularJs

本文关键字:消息 位置 UI-Router AngularJs 设置 使用      更新时间:2023-09-26

我目前有一个简单的控制器检查,看看是否有什么是真或假。如果它为假,我们将用户发送到另一个页面。它看起来像这样:

  if (ok === false){
    $location.path('/follow');
  }
  else
  {
  do stuff brah
  }

我正在使用UI-Router ATM,但我不确定这是否重要。我希望发生的是,如果它被触发,后续页面将显示一条消息,内容类似于"嘿,我们希望您在继续之前填写此信息!"

然而,棘手的部分是,如果用户自己去/跟随,他们不希望出现任何消息。只有当控制器从另一个页面触发时才会发生这种情况。有意义吗?

你有多个选择

构建一个服务并在两个控制器中注入它来存储一个变量是一回事。

你也可以像这样在url中传递信息:

$location.path('/follow').search('showForm', 'true');

然后在创建/follow模块时读取url:

if( $location.search().showForm === true ){
  // do the things brah
  $location.search('showForm', null);
}

你也可以使用来自ui-router的状态改变事件与fromParams和topparams来传输信息:https://github.com/angular-ui/ui-router/wiki我从来没有试过,但应该可以。