使用 UI-Route 将解析结果传递给控制器 [英] Passing result of resolve into controller with UI-Route

查看:20
本文介绍了使用 UI-Route 将解析结果传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在 UI-Router 中使用解析,但我认为我遗漏了一条信息,因为我不知道如何使它们工作.

我有一个这样的状态集:

app.config(['$stateProvider', function($stateProvider) {$stateProvider.state('testState', {url: '/testRoute',控制器:'测试控制器',意见:{身体": {templateUrl: "testHtml.html"}},解决: {测试:函数(){返回{值:测试"};}}})}]);

然后我有一个控制器:

app.controller("TestController", ["$scope", "test", function($scope, test) {控制台日志(测试);}]);

然后我有一个 testHtml.html 部分文件,目前没有任何内容:

测试内容

然后加载到 index.html 的 ui-view 中:

<div ui-view="body" autoscroll></div>

我已经摆弄了一个小时左右,并在谷歌上搜索,但我无法弄清楚我应该做什么才能下定决心做某事并将结果传递给控制器​​.

解决方案

当你在 state 级别选项中提到 views 属性时,它会忽略 templateUrl &controller 在那个状态.它只需要控制器 &template/templateUrl 来自其中一个视图.

代码

 视图:{身体": {templateUrl: "testHtml.html",控制器:'TestContoller'//将其移动到命名视图级别}},

I'm trying to learn how to use resolves with UI-Router, and I think there's a piece of information I'm missing, because I can't figure out how to make them work.

I have a state set like this:

app.config(['$stateProvider', function($stateProvider) {
    $stateProvider
        .state('testState', {
            url: '/testRoute',
            controller: 'TestContoller',
            views: {
                "body": { 
                    templateUrl: "testHtml.html" 
                }
             },
            resolve: {
                test:  function(){
                    return {value: "test"};
                 }
            }
        })      
}]);

And then I have a controller:

app.controller("TestController", ["$scope", "test", function($scope, test) {
    console.log(test);
}]);

then I have a testHtml.html partial file that doesn't have anything in at the moment:

<div ng-controller="TestController">
Test content
</div>

And that gets loaded into the ui-view in index.html:

<div ui-view="body" autoscroll></div>

I've been fiddling around with this for an hour or so now and googling around and I can't quite figure out what I should be doing to get the resolve to do something and pass the result into the controller.

解决方案

When you mention views properties on state level options, it ignores templateUrl & controller on that state. It only take controller & template/templateUrl from one of view.

Code

views: {
   "body": { 
      templateUrl: "testHtml.html",
      controller: 'TestContoller' //moved it to named-view level
   }
},

这篇关于使用 UI-Route 将解析结果传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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