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

AngularJS 1.0路由失效求助:ng-view无法加载模板

Fixing AngularJS 1.x Routing Failure (Template Not Showing in ng-view)

Hey there, let's troubleshoot your routing issue step by step. From the details you shared, here are the most likely culprits and fixes:

1. Mismatched Angular Core and Angular-Route Versions

Looking at your package.json, you're using angular ^1.4.3 but angular-route 1.6.10—this is a critical mismatch! AngularJS requires the routing module version to match the core Angular version exactly. Mixing versions causes unexpected behavior, including broken routing.

Fix: Update your package.json to align the versions:

"dependencies": {
  "angular": "^1.4.3",
  "angular-route": "^1.4.3", // Match this to your Angular core version
  // ... other dependencies remain the same
}

Then run npm install to get the correct version.

2. Missing 'ngRoute' Module Dependency

Your app module must explicitly depend on ngRoute to enable routing. Check your app.js code—you should have:

'use strict';
var app = angular.module('application', ['ngRoute']); // 'ngRoute' must be in the dependencies array

If you omitted ['ngRoute'], Angular won't load the routing functionality at all.

3. Verify $routeProvider Configuration

Even if the module is set up, incorrect route definitions will break things. Make sure your config block looks something like this (adjust paths/controllers to match your app):

app.config(function($routeProvider) {
  $routeProvider
    // Example route for your home page
    .when('/', {
      templateUrl: '/templates/home.html', // Ensure this path is correct relative to your server root
      controller: 'HomeController'
    })
    // Catch-all for undefined routes
    .otherwise({
      redirectTo: '/'
    });
});
  • Double-check the templateUrl path: If the template file isn't found, you'll see a 404 error in your browser's developer console (F12 > Console tab).
  • Ensure the controller specified exists and is properly registered with your app.

4. Confirm ng-view Directive is Present

Make sure you have the ng-view directive in your main HTML file where you want templates to render. For example:

<!DOCTYPE html>
<html ng-app="application">
<head>
  <!-- Your scripts and styles here -->
  <script src="/node_modules/angular/angular.js"></script>
  <script src="/node_modules/angular-route/angular-route.js"></script>
  <script src="/app.js"></script>
</head>
<body>
  <!-- Templates will load here -->
  <div ng-view></div>
</body>
</html>
  • Also, confirm you're loading angular.js before angular-route.js—order matters here.

5. Check Browser Console for Errors

Always start with the browser's developer console! It will tell you if there are:

  • Module loading errors (e.g., "Module 'ngRoute' is not available")
  • 404 errors for missing templates or scripts
  • Syntax errors in your route configuration

Final Notes

After making these changes, restart your server and clear your browser cache to ensure the updated scripts load correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:17:47