You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将ngRoute作为依赖引入后AngularJS代码无法运行,恳请协助排查问题

Troubleshooting AngularJS App Failure After Adding ngRoute Dependency

Hey there, let's work through why your AngularJS app isn't running after adding ngRoute as a dependency. Looking over your code, I’ve spotted a few key issues that are likely causing the problem:

1. Typo in Angular Material CSS File Path

Your link to the Angular Material stylesheet has a typo in the file extension:

<link href="vendor/angular/angular-material.min.cs" rel="stylesheet" media="all">

The extension should be .css (you missed the final "s"). This will cause a 404 error when the browser tries to load the stylesheet, which can disrupt Angular's initialization flow and break your app.

2. Missing Route Configuration

While you’ve injected ngRoute into your module, you haven’t set up any route rules. The <data-ng-view> directive won’t render anything without defined routes, and while this alone might not crash the app, combined with the stylesheet error it can lead to unexpected behavior.

Fixed Code Example

Here’s your code with these issues resolved, plus a basic route configuration to get ng-view working:

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- Angular Material style sheet (fixed extension) -->
<link href="vendor/angular/angular-material.min.css" rel="stylesheet" media="all">
<!-- Angular Material requires Angular.js Libraries -->
<script src='vendor/angular/angular.min.js'></script>
<script src='vendor/angular/angular-route.min.js'></script>
<script src='vendor/angular/angular-aria.min.js'></script>
<script src='vendor/angular/angular-messages.min.js'></script>
<script src='vendor/angular/angular-animate.min.js'></script>
<!-- Angular Material Library -->
<script src='vendor/angular/angular-material.min.js'></script>
</head>
<body>
<h3>HELLO!!! {{2+2}}</h3>
<data-ng-view></data-ng-view>
<script>
var app = angular.module('myApp', ['ngRoute']);

// Basic route configuration
app.config(function($routeProvider) {
  $routeProvider
    .when('/', {
      template: '<p>Welcome to the default route!</p>'
    })
    .otherwise({
      redirectTo: '/'
    });
});
</script>
</body>
</html>

Additional Troubleshooting Steps

  • Check Browser Console: Press F12 to open developer tools, then go to the Console tab. Look for 404 errors (indicating missing files) or Angular-specific errors like Module not available (which would mean angular-route.min.js isn’t loading correctly).
  • Verify Library Versions: Make sure your AngularJS core version matches the version of ngRoute, angular-material, and other related libraries. Mismatched versions often cause compatibility issues.
  • Confirm File Paths: Double-check that all your JS and CSS files exist at the paths you’ve specified. If you’re using a project structure where vendor is in a different location, adjust the paths accordingly.

内容的提问来源于stack exchange,提问作者Abdul_MZaidu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:52:55