【问题标题】:Issue with loading pages with AngularJS routing使用 AngularJS 路由加载页面的问题
【发布时间】:2017-05-09 11:11:59
【问题描述】:

难住了。我的代码没有问题(据我所知),它在没有选择任何内容时加载list-patents.htm模板,但在单击相应的锚标记时无法加载add-patents模板。网址显示如下:

http://localhost/#!/#list-patents

内容未加载的任何原因?可能是网址中的#!

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.min.js"></script>
</head>

<main ng-app="myApp" ng-controller="appCtrl">
    <div class="container">
        <nav>
            <ul>
                <li><a href="#list-patents">List patents</li>
                <li><a href="#add-patent">Add patent</li>
            </ul>
        </nav>
        <section class="ng-view">

        </section>
    </div>
</main>

var app = angular.module('myApp', ['ngRoute']);

app.controller('appCtrl', function($scope, $http, $rootScope) {
    $scope.stages = [
        {'stage': 'green', 'cost': '$1, 500'},
        {'stage': 'yellow', 'cost': '$1, 800'},
        {'stage': 'red', 'cost': '$2, 100'},
        {'stage': 'blue', 'cost': '$2, 500'},
        {'stage': 'brown', 'cost': '$2, 900'}
    ];
    $http.get('../scripts/patent-renewal.json').then(function(response) {
        $scope.application = response.data.patents;
    });

});

app.config(function($routeProvider){
    $routeProvider
    .when('/', {
        templateUrl: '../templates/list-patents.htm',
        controller: 'appCtrl'
    })
    .when('/add-patent', {
        templateUrl:'../templates/add-patent.htm',
        controller: 'appCtrl'
    })
});

【问题讨论】:

    标签: javascript html angular routing


    【解决方案1】:

    !# 可能确实是问题所在,不知道为什么要添加它。

    你可以尝试切换到html5模式

      $locationProvider.html5Mode(true);
    

    然后你的href可以是直接的,没有#

    <li><a href="list-patents">List patents</li>
    <li><a href="add-patent">Add patent</li>
    

    【讨论】:

    • 谢谢!它可以工作,必须指定一个基本 URL,并如您所说删除 #。仍然想知道为什么会出现问题......无论如何,再次感谢
    猜你喜欢
    • 2021-05-05
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2017-08-29
    • 2017-02-24
    相关资源
    最近更新 更多