AngularJS-如何从ui路由器引用子模块控制器?

问题描述 投票:3回答:1

我在Angular 1.3.9应用程序中拥有子模块有些麻烦。我在http://plnkr.co/edit/XBfTPAGRRe0CWJgjGJzc?p=preview上有一个(无法正常工作,很抱歉)预览,我认为这很奇怪,部分原因是我正在使用Restangular。

我有以下内容:

angular
    .module('estimate', ['ui.router', 'restangular', 'estimate.project'])
;

angular
.module('estimate.project', ['ui.router'])
.config(['$stateProvider', '$urlRouterProvider', '$locationProvider'
        , function($stateProvider, $urlRouterProvider, $locationProvider) {

    $stateProvider
       .state('project', {
            url: "/project/{id:int}",
            abstract: true,
            templateUrl: '/app/templates/project.html',
            controller: "ProjectController as project", 
            resolve: { // stuff }
        })
        .state('project.overview', {
            url: "",
            templateUrl: "/app/templates/overview.html"
        })
        // ...
    ;
}])
.controller('ProjectController', ['$scope', 'ProjectService', 'myProject'
          , function($scope, ProjectService, myProject) {

    console.log('i made it!');

}]);

并且在我的模板中,该模板由estimate模块提供:

<li><a ui-sref="project.overview({ id: 1 })">One</a></li>

URL可以在页面上正确解析,但是单击它不会执行任何操作。它甚至不会引发任何控制台错误-它就在那里。我的直觉告诉我,这与我指的是控制器和/或路由以及是否需要对它们进行前缀或修改以使用子模块有关。有关如何使其正确加载的任何想法?

如果这篇文章过于分散,请告诉我,我会尽力清理它。

javascript angularjs angular-ui-router
1个回答
3
投票

I updated your plunker here,并且说,最重要的更改是-在主模块中引用子模块] >>:

代替此:

angular
    .module('estimate', ['ui.router', 'restangular'])
    ...

angular
    .module('estimate.project', ['ui.router'])
    ...

我们必须使用它,即父模块中的引用子模块

angular
    .module('estimate', ['ui.router', 'restangular', 'estimate.project'])
    ...

angular
    .module('estimate.project', ['ui.router'])
    ...

还有一些其他小的调整,应该是方式

。检查其是否正常运行here
© www.soinside.com 2019 - 2024. All rights reserved.