简体   繁体   English

MEAN角度ui路由器未加载状态

[英]MEAN angular ui-router doesn't load states

I am building a website using meanstack. 我正在使用meanstack建立网站。 However, it failed to load any state I defined in app.js. 但是,它无法加载我在app.js中定义的任何状态。 And there is no errors shown in console, and every js file is loaded successfully. 而且控制台中没有显示任何错误,并且每个js文件都已成功加载。 Here is my index.html: 这是我的index.html:

<html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <base href="/"></base>
    <title>E-study</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" type="text/css" href="angular-loading-bar/build/loading-bar.css">
    <link rel="stylesheet" type="text/css" href="bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="app.less">
    <!-- <link rel="stylesheet" type="text/css" href="app/_naut/less/styles.less"> -->
    </head>
    <body ng-app="E-study">
        <div id="main">
            <div id="topbar" class="topbar" style="width: auto;">
                <div class="fill">
                    <div class="container">
                        <h1 style="align: center;">E-study - a website for English self study</h1>
                    </div>
                </div>
            </div>
            <div data-ui-view data-autoscroll="false"  class="app-container ng-scope"></div>
       </div>
   </body>
</html>

app.js: app.js:

'use strict';

var Estudy = angular.module('E-study', ['ngCookies', 'ngResource', 'ngSanitize', 'ui.router', 'naut']);
Estudy.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', '$httpProvider', 'RouteProvider', function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider, Route){
    $stateProvider
    .state('login', {
        url: '/login',
        templateUrl: 'client/views/login.html',
        controller: 'loginCtrl'
    })
    .state('signup', {
        url: '/signup',
        templateUrl: 'client/views/signup.html',
        controller: 'signupCtrl'
    })
    .state('recover', {
        url: '/resetPwd',
        templateUrl: 'client/views/recover.html',
        controller: 'recoverCtrl'
    });
    $urlRouterProvider.otherwise('/login');
    $locationProvider.html5Mode(true);
    $httpProvider.interceptors.push('authInterceptor');
}])
.factory('authInterceptor', function ($rootScope, $q, $cookieStore, $location){
    return{
        request: function (config){
            config.headers = config.headers || {};
            if ($cookieStore.get('token')) {
                config.headers.Authorization = 'Bearer' + $cookieStore.get('token');
            };
            return config;
        },

        responseError: function (response){
            if (response.status === 401) {
                $location.path('/login');
                $cookieStore.remove('token');
                return $q.reject(response);
            } else {
                return $q.reject(response);
            };
        }
    };
})
.run(function($state){
     $state.go('login');  
 });
});

I run the server.js in the root directory, the whole file directory of my project is: file directory 我在根目录中运行server.js,我项目的整个文件目录为: 文件目录

Thanks. 谢谢。

You need configure the ng-app in <html lang="en"> tag . 您需要在<html lang="en">标签中配置ng-app and also you need to call (reference) all js files in index.html which you want to run. 并且您还需要调用(引用)要运行的index.html中的所有js文件。

If you have inject 3 controller's in 3 js files. 如果您在3个js文件中注入了3个控制器。 you should be referred the js file in your index.js file. 您应该在index.js文件中引用该js文件。

You have not included the script in the html..that the cause of the error. 您尚未将脚本包含在html ..那是导致错误的原因。

just paste the code 只需粘贴代码

<script src="/app.js"></script>

before </head> tag so that your code becomes </head>标记之前,以便您的代码成为

<head>
<!---here leave the code same and just add this bottom script tag -->
<script src="/app.js"></script>
</head>

the above code will only work if your app.js is in /var/www/html directory and if the it is in another directory say... /var/www/html/folder1/ then you have to use.. 上面的代码仅在您的app.js位于/var/www/html目录中并且如果位于另一个目录中时才起作用,例如... /var/www/html/folder1/那么您必须使用。

<script src="/folder1/app.js"></script>

在您的index.html文件中添加此

<main ui-view></main>

I have found the problem, that is I defined duplicate route 我发现了问题,即我定义了重复路线

app.use('/', function(req, res, err){ res.sendFile('index.html', {root: path.join(__dirname, '../client')}) })

in the routes.js file, and put it before 在routes.js文件中,并将其放在

app.route('/*').get(function (req, res){ res.sendFile('index.html', {root: path.join(__dirname, '../client')}); });

After I delete the first code, it loads the state correctly. 删除第一个代码后,它将正确加载状态。

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

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