Passing result of resolve into controller with UI-Route

这一生的挚爱 提交于 2019-12-11 11:58:34

问题


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.


回答1:


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
   }
},


来源:https://stackoverflow.com/questions/36105347/passing-result-of-resolve-into-controller-with-ui-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!