简体   繁体   English

Angular ui-router不加载控制器或模板

[英]Angular ui-router not loading controller or template

I seem to have an issue getting ui-router to actually route things. 我似乎有一个问题让ui-router实际路由东西。 I am sure that all of my javascript files are being loaded and angular isn't throwing any errors. 我确信我的所有javascript文件都被加载,而角度不会丢失任何错误。 I have an HTML file that declares the app and base controller and then I load the js file that has the router. 我有一个HTML文件,声明应用程序和基本控制器,然后我加载具有路由器的js文件。 You can see a sample of my code below. 您可以在下面看到我的代码示例。

index.html 的index.html

<!DOCTYPE html>
<html ng-app="Yellr" ng-controller="yellrBaseCtrl">
<head>
    <title>Yellr Moderator</title>
    <link rel="stylesheet" type="text/css" href="assets/css/site.css"/>
</head>
<body>
    <div class="side-nav">
...
    </div>
    <div class='main' ui-view>
    </div>

    <script src="assets/js/scripts.min.js"></script>
</body>
</html>

yellr.routes.js (compiled into scripts.min.js) yellr.routes.js(编译成scripts.min.js)

'use strict';

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

        $urlRouterProvider.otherwise('/notfound');

        $stateProvider
            .state('feed', {
                url: '/feed',
                templateUrl: '/templates/feed.html',
                controller: 'rawFeedCtrl'
            });
    }]);

console.log('Yellr routes');

Am I missing something obvious here? 我错过了一些明显的东西吗? You can find the whole code base here 你可以在这里找到整个代码库

The problem was with template url. 问题出在模板网址上。 I see you are serving the files directly from your root folder, not from the app. 我看到你直接从你的根文件夹提供文件,而不是从应用程序。 You will have to change the template url to this: 您必须将模板网址更改为:

$stateProvider
   .state('feed', {
        url: '/feed',
        templateUrl: 'app/templates/feed.html',
         controller: 'rawFeedCtrl'
    });

Also I would suggest to build all the html and scripts into a dist folder and serve from there. 另外我建议将所有的html和脚本构建到dist文件夹中并从那里开始服务。

I created working plunker here. 这里创造了工作的plunker

I added the reference to angular and ui-router 我添加了对angular和ui-router的引用

<!DOCTYPE html>
<html ng-app="Yellr" ng-controller="yellrBaseCtrl">

  <head>
    <title>Yellr Moderator</title>

    <link rel="stylesheet" type="text/css" href="assets/css/site.css" />
  </head>

  <body>
    <div class="side-nav">
...
    </div>
    <div class="main" ui-view=""></div>

    <script data-require="angular.js@*" data-semver="1.3.7" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js"></script>
    <script data-require="ui-router@*" data-semver="0.2.13" src="//rawgit.com/angular-ui/ui-router/0.2.13/release/angular-ui-router.js"></script>

    <script src="yellr.routes.js"></script>
    <script src="yellrBaseCtrl.js"></script>
    <script src="rawFeedCtrl.js"></script>
  </body>

</html>

and changed the otherwise: 并改变了否则:

$urlRouterProvider.otherwise('/feed');

so this state is loaded on app start: 所以在app start上加载了这个状态:

$stateProvider
    .state('feed', {
        url: '/feed',
        templateUrl: 'feed.html',
        controller: 'rawFeedCtrl'
    });

The rest is as in your case... working. 其余的就像你的情况一样......工作。 Check it here 在这里查看

The first thing I would suggest is that, given ui-router is handling the routes and assigning controllers, there may be a conflict with you assigning a controller on the html element 我建议的第一件事是,鉴于ui-router正在处理路由并分配控制器,可能与你在html元素上分配控制器有冲突

<html ng-app="Yellr" ng-controller="yellrBaseCtrl">

Using some sample code from a project i'm working on: 使用我正在处理的项目中的一些示例代码:

In index.html: 在index.html中:

<html ng-app="ddsWeb">
....
<!--header-->
<div ui-view="header"></div>

<!--content area-->
<div ui-view="content" class="container-fluid slide"></div>

<!--footer-->
<div ui-view="footer"></div>
...

In app.js: 在app.js中:

var ddsWeb = angular.module('ddsWeb',
    [
        'ui.router',
...
    ]);


// configure states
ddsWeb.config(function ($stateProvider, $urlRouterProvider) {

    // For any unmatched url, redirect to /state1
    $urlRouterProvider.otherwise("/customers");

    // Now set up the states
    $stateProvider
        .state('customers', {
            abstract: true,
            url: "/customers",
            views: {
                header: {
                    templateUrl: "/project/partials/header.html"
                },
                content: {
                    templateUrl: "/project/partials/customers/customers.html"
                },
                footer: {
                    templateUrl: "/project/partials/footer.html"
                }
            }
        })

        .state('customers.list', {
            url: '',
            views: {
                'list@customers': {
                    templateUrl: "/project/partials/customers/customers.list.html",
                    controller: "customerListController"
                },
                'searchbar@customers': {
                    templateUrl: "/project/search/searchbar.html",
                    controller: "searchController"
                },
                'pagination@customers': {
                    templateUrl: "/project/pagelink/pagination.html",
                    controller: "pageLinkController"
                }
            }
        })

        .state('customers.detail', {
            url: '/detail/{customerId}',
            views: {
                'detail_modal@customers': {
                    controller: 'customerDetailController'
                }
            }
        })

});

Then, for example, in CustomerListController: 然后,例如,在CustomerListController中:

ddsWeb.controller('customerListController', function ($scope,
                                                      pageLinkService,
                                                      searchService) {

    searchService.setType('customerSearch');
    pageLinkService.setType('customerPageChange');

    $scope.getCustomers = function () {
        $scope.CustomerModel.getAll(pageLinkService.current_page, pageLinkService.per_page, searchService.searchText)
            .then(function (result) {
                pageLinkService.current_page = Number(result.data.current_page);
                pageLinkService.last_page = Number(result.data.last_page);
                pageLinkService.calculatePages();
            });
    };

    $scope.$on('customerSearch', function () {
        pageLinkService.resetPages();
        $scope.getCustomers();
    });

    $scope.$on('customerPageChange', function () {
        $scope.getCustomers();
    });

    $scope.getCustomers();

});

Please note that my code is not "properly" modularized; 请注意,我的代码没有“正确”模块化; there's too much in one module (ddsWeb), and i'm planning on fixing that. 在一个模块(ddsWeb)中有太多,我正计划修复它。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM