简体   繁体   English

角路由ngRoute无法提取我的其他HTML文件

[英]Angular Routing ngRoute fails to pull my other HTML files

So after reading here and there on MEAN, I decided to make my first MEAN application which is pretty small, all seems to work except the routeing to make my app a one-page app. 因此,在四处浏览MEAN之后,我决定制作我的第一个MEAN应用程序,该应用程序非常小,除了路由使我的应用程序成为一页应用程序之外,其他所有功能似乎都可以使用。 Any help would be much appreciated! 任何帮助将非常感激!

 app.config(function($routeProvider){
   $routeProvider
     .when('/', {
       templateUrl: 'main.html',
       controller: 'mainController'
     })
     .when('/login', {
       templateUrl: 'login.html',
       controller: 'authController'
     })
     .when('/register', {
       templateUrl: 'register.html',
       controller: 'authController'
     });
 });

Anyway, I adopted a boilerplate navigation bar 无论如何,我采用了样板导航栏

       <nav class="navbar-fluid navbar-default navbar-fixed-top">
         <div class="container">
           <a class="navbar-brand" href="#">IoT</a>
           <p class="navbar-text">IoT</p>
           <p class="navbar-right navbar-text" ng-hide="authenticated"><a href="#/login">Login</a> or <a href="#/register">Register</a></p>
           <p class="navbar-right navbar-text" ng-show="authenticated"><a href="#" ng-click="signout()">Logout</a></p>
           <p class="navbar-right navbar-text" ng-show="authenticated">Signed in as {{current_user}}</p>
         </div>
       </nav>

And, when I run it on localhost:3000, the homepage address I got instead is 而且,当我在localhost:3000上运行它时,我得到的主页地址是

 http://localhost:3000/#!/

where I was expecting 我期待的地方

 http://localhost:3000/#/

And when I clicked on the 'register' link, the address I got is 当我点击“注册”链接时,我得到的地址是

 http://localhost:3000/#!/#%2Fregister

where as I was expecting 如我所料

 http://localhost:3000/#/register

Is that normal? 那是正常的吗? Maybe it's bcs of the version of Angular I was using? 也许是我使用的Angular版本的bcs?

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular-resource.min.js"></script>

It was all fine before, but then I stripped down the HTML and make the main page pull individual HTML pages one-by-one then this happened. 之前一切都很好,但是后来我剥离了HTML,使主页一页一页地拉动各个HTML页面,然后发生了这种情况。

Change your links from <a href="#/login"> to <a href="#!/login"> , <a href="#"> to <a href="#!"> etc.. 将您的链接从<a href="#/login">更改为<a href="#!/login"> ,将<a href="#">更改为<a href="#!">等。

I also have this issue but changing from a hash to hashbang resolves it for me. 我也有这个问题,但是从哈希更改为hashbang可以为我解决。

App.config(['$routeProvider', '$locationProvider', function config($routeProvider, $locationProvider) {  
    $locationProvider.hashPrefix('')

    $routeProvider
        .when('/', {
            templateUrl: 'login.html',
            controller: 'loginController'
        })
        .otherwise('/');
}]);

will work see https://docs.angularjs.org/api/ng/provider/ $locationProvider 可以使用,请参见https://docs.angularjs.org/api/ng/provider/ $ locationProvider

Try this one. 试试这个。 I'm not sure this will fix your issue. 我不确定这是否可以解决您的问题。 But please try this one. 但是请尝试这个。

Change your route provider by including default parameter. 通过包括默认参数来更改您的路由提供者。

app.config(function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'main.html',
                controller: 'mainController'
            })
            .when('/login', {
                templateUrl: 'login.html',
                controller: 'authController'
            })
            .when('/register', {
                templateUrl: 'register.html',
                controller: 'authController'
            })
            .otherwise({
                redirectTo: '/'
            });
    });

and provide the links like <a href="#/login">Login</a> 并提供<a href="#/login">Login</a>类的链接

Try this one: 试试这个:

var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider
    .when('/', {
       templateUrl: 'main.html',
       controller: 'mainController'
     })
     .when('/login', {
       templateUrl: 'login.html',
       controller: 'authController'
     })
     .when('/register', {
       templateUrl: 'register.html',
       controller: 'authController'
     })
     .otherwise({
       redirectTo: '/'
     });
});

For Sign Out, You should use such as: 对于注销,您应该使用例如:

<p class="navbar-right navbar-text" ng-show="authenticated"><a href="#/" ng-click="signout()">Logout</a></p>

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

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