angular.js - angular-ui-router的预加载与控制器注入
ringa_lee
ringa_lee 2017-05-15 17:11:59
0
2
473

ui-router的预加载 resolve获得数据后,如何向控制器注入?

备注:

  • $state中,我通过controllerUrl引入控制器文件。

  • 我的ui-router版本为0.4.2

路由代码 ↓

$stateProvider.state('dashboard', { url: '/dashboard', templateUrl: 'views/dashboard/index.html', resolve: { getList: function ($stateParams, $NetRequest){ var options = {type: 'charts'}; $NetRequest.post(options).then(function($ref){ return $ref.data; }); } }, controllerUrl: 'views/dashboard/index', controller: 'DashboardController', controllerAs: 'vm' });

服务代码 ↓

service.post = function($options){ var deferred = $q.defer(); // 省略请求函数... return deferred.promise; };

这时候如果直接在控制器中注入getList,结果为undefined

我看官方文档介绍,有个$inject的参数,可惜没有实例,我不清楚该如何使用。

ringa_lee
ringa_lee

ringa_lee

reply all (2)
仅有的幸福

The problem is solved, the method is actually very simple - -

$stateProvider.state('dashboard', { url: '/dashboard', templateUrl: 'views/dashboard/index.html', resolve: { getList: function ($stateParams, $NetRequest){ var options = {type: 'charts'}; return $NetRequest.post(options); // 因为 $NetRequest.post 已经返回了结果 } }, controllerUrl: 'views/dashboard/index', controller: 'DashboardController', controllerAs: 'vm' });
    漂亮男人

    '//Get data here'. . . . Did you return your promise?

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!