AngularJS无法路由到不同的页面

时间:2017-02-14 19:13:23

标签: javascript angularjs

我有一个简单的Web项目,它有一个电话列表,该列表中的每个元素都包含一个电话详细信息。如果我点击了手机ID,它应该只会将我带到该手机的页面,但是,在点击手机ID后没有任何反应。

当应用程序加载后,它会使用url:

进入索引页面

http://localhost:8080/angular-route-multiple-views/index.html#!/phones

点击第一个手机ID后,网址已更改为以下内容,但根本没有进入该页面:

http://localhost:8080/angular-route-multiple-views/index.html#!/phones#%2Fphones%2Fnexus

有人可以帮忙,让我知道它为什么没有进入手机详情页面?感谢。

的index.html

<!DOCTYPE html>
<html ng-app="mainModule">
<head>
<meta charset="ISO-8859-1">
<title>AngularJS Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-route.js"></script>
<script src="js/main-module.js"></script> 
<script src="js/phonelist-module.js"></script> 
<script src="js/phonelist-component.js"></script> 
<script src="js/config.js"></script>
<script src="js/phonedetail-module.js"></script>
<script src="js/phonedetail-component.js"></script>         
</head>
<body>
    <div ng-view></div>
</body>
</html>

主要-module.js:

angular.module('mainModule', ['ngRoute', 'phoneList', 'phoneDetail']);

config.js:

angular.module('mainModule').
 config(['$locationProvider', '$routeProvider', 
      function config($locationProvider, $routeProvider) {
      $locationProvider.hashPrefix('!');

      $routeProvider.
      when('/phones', {
         template: '<phone-list></phone-list>'
      }).
      when('/phones/:phoneId', {
        template: '<phone-detail></phone-detail>' 
      }).
      otherwise('/phones');
  }
]);

PHONELIST-module.js:

angular.module('phoneList', ['ngRoute']);

PHONELIST-component.js:

 angular.module('phoneList').component('phoneList', {
 templateUrl: 'js/phone-list.template.html',  
  controller: function PhoneListController() {
  this.phones = [
    {
      id: 'nexus',
      name: 'Nexus S',
      snippet: 'Fast just got faster with Nexus S.',
    },
    {
      id: 'motorola-xoom',
      name: 'MOTOROLA XOOM™',
      snippet: 'The Next, Next Generation tablet.',
    }
  ];
}
});

phonedetail-module.js:

angular.module('phoneDetail', ['ngRoute']);

phonedetail-component.js:

angular.module('phoneDetail').
  component('phoneDetail', {
     template: 'TBD: Detail view for <span>{{$ctrl.phoneId}}</span>',
     controller: ['$routeParams',
     function PhoneDetailController($routeParams) { 
        this.phoneId = $routeParams.phoneId;
    }
  ]
 });

电话list.template.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Phone List</title>
</head>
<body>
    <li ng-repeat="phone in $ctrl.phones" >
    <a href="#/phones/{{phone.id}}">{{phone.name}}</a>
    <p>{{phone.snippet}}</p>
   </li>
 </ul>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您忘记在!属性中添加href

<ul>
    <li ng-repeat="phone in $ctrl.phones">
        <a href="#!/phones/{{phone.id}}">{{phone.name}}</a>
        <p>{{phone.snippet}}</p>
    </li>
</ul>