简体   繁体   English

角度路线问题-路线未执行

[英]Angular-route issues - Route not executing

I've been following a set of tutorials from Scott Allen. 我一直在关注Scott Allen的一组教程。 I have tried to emulate what he does down to the bone I believe however my angular route is just not being executed. 我试图模仿他所做的事情,直到我相信,但是我的棱角路线并没有被执行。

I have a view called main.html and a shell/layout view called index.html. 我有一个名为main.html的视图和一个名为index.html的shell /布局视图。 Inside the index.html I have ng-view in the body tag and I have a route in a script file called app.js as follows: 在index.html中,我在body标签中具有ng-view,并且在名为app.js的脚本文件中具有一条路由,如下所示:

(function(){

  var app = angular.module("githubViewer", ["ngRoute"]);

  app.config(function($routeProvider){
    $routeProvider
        .when("/main", {
          templateUrl: "main.html",
          controller: "MainController"
        })
        .when("/user/:username", {
          templateUrl: "user.html",
          controller: "UserController"
        })
        .otherwise({redirectTo:"/main"});
  });

}());

Here is the index.html's script imports 这是index.html的脚本导入

 <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
    <script src="https://code.angularjs.org/1.2.20/angular-route.js"></script>

The way I know that the route isn't being hit is because the main.html's code isn't being passed back to the page. 我知道没有被点击的方法是因为main.html的代码没有被传递回页面。

Has something perhaps changed in the way the angular-route is used? 角度路线的使用方式是否有所改变?

Also here is a link to my plunk : http://plnkr.co/edit/Ew73QexRoDZlrvPG2UA1?p=preview 另外,这是我的小精灵的链接: http ://plnkr.co/edit/Ew73QexRoDZlrvPG2UA1? p= preview

You are creating same angular module multiple times. 您将多次创建相同的角度模块。 Creating same angular module number of times just fails. 创建相同的角度模块次数失败。 So, create angular module once and use it in other places as follows 因此,只需创建一次角度模块,然后在其他地方使用它,如下所示

var app = angular.module('FirstModule');  // app.js
app.controller // controller.js
app.factory  // service.js
app.directive  // directives.js

Here is the updated plunkr. 这是更新的plunkr。

http://plnkr.co/edit/qWHkzj9v6nGhhb7MjDDf?p=preview http://plnkr.co/edit/qWHkzj9v6nGhhb7MjDDf?p=预览

I have changed the following self executing function to normal functions as they create closures and the module created in app.js is not available in other files. 我将以下自执行函数更改为普通函数,因为它们创建了闭包,而app.js中创建的模块在其他文件中不可用。

(function(){

}());

Otherwise, you can pass module created in app.js as parameter to all self executing functions in other files. 否则,您可以将app.js中创建的模块作为参数传递给其他文件中的所有自执行函数。

Fixed the plunker . 修复了矮人 You used to call angular.module again and as a setter instead of using it as a getter. 您过去angular.module再次调用angular.module作为设置器,而不是将其用作获取器。

EDIT: I'm one minute late =) You can use Vikram Babu's answer and remove the wrapping closures completely. 编辑:我迟到了一分钟=)您可以使用Vikram Babu的答案并完全除去包装瓶盖。 IF you need to use the closures just use var app = angular.module("githubViewer"); 如果您需要使用闭包,则只需使用var app = angular.module("githubViewer"); instead of var app = angular.module("githubViewer", []); 而不是var app = angular.module("githubViewer", []); in order not to overwrite the module definition. 为了不覆盖模块定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM