Unexpected result using angular-routes

时间:2018-12-24 16:35:52

标签: angularjs

I am working through the book "MEAN web development by Amos Haviv" and I am not getting the expected results in a particular portion of the chapter on AngularJS routing. This section of the book is on setting up angular routing for a single page application. In short I expect that when I navigate to http://localhost:3000,AngularJS路由器应将请求重定向到http://localhost:300/#!/,并从我的一个视图中看到两个文本框。

我严格按照书中给出的代码进行操作,并多次检查代码以检查是否错过了某些内容。本书中的过程如下: -在public / application.js文件中配置应用程序路由 -使用ngRoute模块在public / example / example.client.routes.js文件中管理路由 -在view.html文件中包括脚本并添加 -最后运行服务器

我的application.js文件:

var mainApplicationModuleName = 'mean';

var mainApplicationModule = angular.module(mainApplicationModuleName,
    ['ngRoute', 'example']);

    mainApplicationModule.config(['$locationProvider',
    function($locationProvider) {
    $locationProvider.hashPrefix('!');
    }
    ]);

angular.element(document).ready(function() {
    angular.bootstrap(document, [mainApplicationModuleName]);
});

我的example.client.routes.js文件:

angular.module('example').config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'example/views/example.client.view.html'
}).
otherwise({
redirectTo: '/'
});
}
]);

我的example.client.view.html文件:

<section ng-controller="ExampleController">
    <input type="text" id="text1" ng-model="name">
    <input type="text" id="text2" ng-model="name">
</section>

在我的index.ejs文件中,我除了包括angular之外,还包括<section ng-view></section>和脚本标记,例如example.client.routes.js,example.client.controller.js,example.client.module.js。 js和angular-route.js

启动节点服务器时,我在控制台日志中看到以下请求:

  

服务器正在运行
  GET / 304 32.496 ms--
  GET /img/logo.png 404 11.459毫秒-25
  GET /lib/angular/angular.js 304 15.638毫秒--
  GET /lib/angular-route/angular-route.js 304 12.533毫秒--
  GET /example/example.client.module.js 304 14.989毫秒--
  GET /example/controllers/example.client.controller.js 304 15.237毫秒--
  GET /example/config/example.client.%20%20%20%20%20%20%20%20%20routes.js 404 15.647 ms-76

根据这本书,我应该重新定向到http://localhost:3000/#!/,并且应该看到属于我观点的两种形式,但是这不会发生。该页面仅保留在http://localhost:3000/,我看不到表格。在上面的日志中,我无法弄清楚路由为何显示为

  

获取/example/config/example.client.%20%20%20%20%20%20%20%20%20routes.js

而不是

  

获取/example/config/example.client.routes.js

0 个答案:

没有答案