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

时间:2014-05-20 17:51:06

标签: javascript angularjs angular-ui-router

我目前有一个简单的控制器,可以检查某些内容是真还是假。如果错误,我们将用户发送到不同的页面。看起来有点像这样:

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

我使用的是UI-Router ATM,但我不确定这是否重要。我想要发生的是,如果这被触发,后续页面将显示一条消息,其中包含"嘿,我们希望您在前进之前填写此消息!&#34 ;

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

1 个答案:

答案 0 :(得分:1)

您有多种选择。

构建服务并将其注入两个控制器以存储变量是一个。

你也可以像这样在网址中传输信息:

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

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

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

您也可以使用来自ui-router的状态更改事件,使用fromParams和toParams来传输信息:https://github.com/angular-ui/ui-router/wiki 我从来没有尝试,但它应该工作。