【问题标题】:AngularJS Routed Page Not ShowingAngularJS路由页面不显示
【发布时间】:2016-01-05 22:39:55
【问题描述】:

这么快的问题我一直在看这段代码但找不到原因,它只是给了我页面上的输出

Partials/View1.html

我并不是想在遇到一些小问题时就在这里发帖,但这已经让我困惑了一段时间,而且我在控制台上没有收到任何错误。我从http://plnkr.co/edit/sN9TagVBOdX3mkrxaTiu?p=preview 运行代码,在另一篇文章中提到它工作正常,但我没有得到正确的输出。以下是index.html(主页面)

<!DOCTYPE html>
<html ng-app="demoApp">
<head>
</head> 
<body>
    <div ng-view></div>


        <script src="angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>
    <script src="scripts.js"></script>
</body>

我的脚本看起来像:

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

            demoApp.config(function($routeProvider) {
            $routeProvider
                .when('/',
                      {
                        controller: 'SimpleController',
                        template: 'Partials/View1.html'
                      })
                .when('/partial2',
                     {
                        controller: 'SimpleController',
                        template: 'Partials/View2.html'
                    })
                .otherwise({ redirectTo: '/'});
         } );

    demoApp.controller('SimpleController', function ($scope) {
        $scope.customers = [
            {name: 'John Smith', city: 'Phoenix'},
            {name: 'Jane Doe', city: 'New York'},
            {name: 'John Doe', city: 'San Francisco'}
        ];

        $scope.addCustomer = function() {
            $scope.customers.push({ 
                name: $scope.newCustomer.name,
                city: $scope.newCustomer.city

            });
        }
    });

我没有从 view1.html 中得到应该显示的内容,我只是得到了上面显示的内容。这是代码

<div class="container">
<h2>View 1</h2>
Name: 
<br>
<input type="text" ng-model="filter.name">
<br>
<ul>
    <li ng-repeat="cust in customers | filter:filter.name | orderBy: 'city'">{{ cust.name }} - {{ cust.city }}</li>
</ul>

<br>
Customer name: <br>
<input type="text" ng-model="newCustomer.name">
Customer city: <br>
<input type="text" ng-model="newCustomer.city">
<br>
<button ng-click="addCustomer()">Add Customer</button>
<br>
<a href="#/view2">View 2</a>

</div>

【问题讨论】:

    标签: javascript html angularjs routes


    【解决方案1】:

    您应该使用 templateUrl 而不是模板:

    .when('/partial2',
       {
         controller: 'SimpleController',
         templateUrl: 'Partials/View2.html'
       })
    

    https://docs.angularjs.org/api/ngRoute/provider/$routeProvider

    【讨论】:

    • 哇,我害怕这个。看了所有的东西,没有错误,不知何故,在我的脑海里,我认为这将是一件愚蠢的事情。谢谢。
    • 别担心,@theman55,当您花了很长时间查看自己的代码时,您自己几乎不可能发现这些错误。对于没有编写它的人来说,跳进去找到错误要容易得多
    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多