【问题标题】:How can I inject custom data like factory in the ui router stateChangeStart function如何在 ui 路由器 stateChangeStart 函数中注入像工厂这样的自定义数据
【发布时间】:2015-01-15 21:53:47
【问题描述】:

我这样称呼:

$state.go("main.projects.selected.dates.day", { id: $state.params.id }, {reload: true});

我真的很想将自定义数据从 state.go 函数传递到 stateChangeStart 函数。这可以通过状态参数来实现 - 我可以滥用它来接受日期对象 - 但是我的网址将无法正常工作......

如何将 state.go 中的数据传递到 stateChangeStart?仅仅通过破解 rootScope.customData 属性?

$rootScope.$on('$stateChangeStart', function (ev, toState, toParams, fromState, fromParams, injectCustomFactory here)
{
   // Run custom logic here on the injected custom factory

   // then change the toParams.customData property by assigning the injected value to this property.

}

为什么我必须在你可能会问的 stateChangeStart 中运行这个逻辑。

这是因为只有在那里我才能检查用户是否主动更改了 url($rootScope.customData 是假的)或者 state.go 是从外部完成的,在我执行 $rootScope.customData = MyValueLogic 之前;

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    在运行中注入它(或者你正在观看的任何地方$stateChangeStart):

    angular.module('lalala').run(function ($rootScope, factory) {
        $rootScope.on('$stateChangeStart', function (e, toState, ...) {
            factory.doSomething();
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多