简体   繁体   中英

MEAN angular ui-router doesn't load states

I am building a website using meanstack. However, it failed to load any state I defined in app.js. And there is no errors shown in console, and every js file is loaded successfully. Here is my 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:

'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

Thanks.

You need configure the ng-app in <html lang="en"> tag . and also you need to call (reference) all js files in index.html which you want to run.

If you have inject 3 controller's in 3 js files. you should be referred the js file in your index.js file.

You have not included the script in the html..that the cause of the error.

just paste the code

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

before </head> tag so that your code becomes

<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..

<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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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