Angular中的路线不起作用

时间:2016-07-19 11:34:06

标签: angularjs routing angularjs-routing

代码

<body ng-app="ngapp">
<h2>NG App</h2>
<div>
    <ng-view></ng-view> 
</div>
<script src="Scripts/angular.min.js"></script>
<script>
    var app = angular.module('ngapp', ["ngRoute"]);
    app.config(function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'Page1.html',
                controller: 'simpCtrl'
            })
            .when('/view2', {
                templateUrl: 'Page2.html',
                controller: 'simpCtrl'
            });
    });
    app.controller('simpCtrl', function ($scope) {
        $scope.customers = [
            { name: 'Jack', age: 10 },
            { name: 'Abdul', age: 12 },
            { name: 'Zubair', age: 11 },
            { name: 'Ammar', age: 10 }
        ];
    });
</script>

我已经正确注册了路线,但它不起作用,我不知道为什么。任何想法如何调试或找到角度不正常的代码?

Page1.html

<div>
<h2>View 1</h2>
Customer Name: 
    <input type="text" ng-model="search.name" />
<ul>
    <li ng-repeat="n in customers | orderBy:'name' | filter:search:strict ">{{n.name|uppercase}} - {{n.age}}
    </li>
</ul>
<a href="#/Page2.html">View 2</a>

谢谢

Plunker link to code

1 个答案:

答案 0 :(得分:3)

您的申请几乎没有问题,

(i)您正在引用angular2库,将其更改为

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.js"></script>

(ii)您尚未添加ngRoute的参考资料

 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.4/angular-route.js"></script>

工作Application