获得一个国家的名义在其`onEnter`挂钩 [英] Getting the name of a state in its `onEnter` hook

查看:108
本文介绍了获得一个国家的名义在其`onEnter`挂钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设,我想切换属性在服务的用户进入和离开的路线当下的应用程序。要做到这一点,我需要了解在的OnEnter onExit 挂钩的国家的名字。这是比较容易的 onExit 挂钩,因为我可以只注射 $状态服务,并读取当前的名字州。但是,由于目前的状态还没有确定,当的OnEnter 挂钩被称为是没有办法知道我们正在过渡到状态。

我还需要有对国家的其他部分很好的控制,所以我宁愿没有任何for循环。我正在寻找一种方法能够在的OnEnter 函数传递的状态,同时还检索功能本身的内部状态的名字。

下面是code我已经写了:

 函数的OnEnter($状态,步骤){
  VAR Statename的= $ state.current.name; // 不可能。目前国家已尚未设置!
  VAR步= Steps.getByStateName(Statename的);  step.activate();
}功能onExit($状态,步骤){
  VAR Statename的= $ state.current.name; // 没问题。我们知道的状态。
  VAR步= Steps.getByStateName(Statename的);  step.deactivate();
}$ stateProvider
  .STATE('第一步',{
    网址:'/第一步,
    templateUrl:'模板/ step1.html',
    控制器:'StepOneController',
    的OnEnter:的OnEnter,
    onExit:onExit
  });

我的解决办法我用,现在是使用一个工厂来创建的,传递到状态的OnEnter 函数上下文。这是很不理想,因为我还需要国家的名字传递给它。

下面是说解决办法的一个例子:

 函数onEnterFactory(Statename的){
  功能的OnEnter(步骤){
    VAR步= Steps.getByStateName(Statename的);    step.activate();
  }
}$ stateProvider
  .STATE('第一步',{
    网址:'/第一步,
    templateUrl:'模板/ step1.html',
    控制器:'StepOneController',
    的OnEnter:onEnterFactory('第一步')
  });


解决方案

在我的项目之一,我们用这样的事情

  app.run(函数($ rootScope,$状态,$位置){
    $ rootScope。在$('$ stateChangeSuccess',函数(事件,toState,toParams,
    fromState){
    $状态previous = fromState。
  });

记住previous状态。但是,你可能还记得,新州和地方存储信息。

I'm building an application where I want to toggle a property in a service the moment a user enters and leaves a route. To do this I need to know about the state's name in the onEnter and onExit hooks. This is relatively easy for the onExit hook since I can just inject the $state service and read the name of the current state. But since the current state has not been set yet when the onEnter hook is called there is no way of knowing what the state we're transitioning to.

I still need to to have fine control over other parts of the state so I'd rather not have any for loops. I'm looking for a way to be able to pass the onEnter function to the state, whilst still retrieving the state's name inside of the function itself.

Here is the code I've written:

function onEnter($state, Steps) {
  var stateName = $state.current.name; // Not possible. The current state has not been set yet! 
  var step = Steps.getByStateName(stateName);

  step.activate();
}

function onExit($state, Steps) {
  var stateName = $state.current.name; // No problem. We know about the state. 
  var step = Steps.getByStateName(stateName);

  step.deactivate();
}

$stateProvider
  .state('step1', {
    url: '/step1',
    templateUrl: 'templates/step1.html',
    controller: 'StepOneController',
    onEnter: onEnter,
    onExit: onExit
  });

My solution I'm using for now is to use a factory to create context for the onEnter function passed to the state. This is far from ideal because I still need to pass the state's name to it.

Here is an example of said workaround:

function onEnterFactory(stateName) {
  function onEnter(Steps) {
    var step = Steps.getByStateName(stateName);

    step.activate();
  }
}

$stateProvider
  .state('step1', {
    url: '/step1',
    templateUrl: 'templates/step1.html',
    controller: 'StepOneController',
    onEnter: onEnterFactory('step1')
  });

解决方案

In one of my projects we used something like this

app.run(function($rootScope, $state, $location) {
    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams,
    fromState) {
    $state.previous = fromState;
  });

to remember the previous state. But you might as well remember the new state and store the information somewhere.

这篇关于获得一个国家的名义在其`onEnter`挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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