AngularJS 1.6路由不工作

时间:2021-09-15 19:41:40

I am trying to create my first angular app. But its not working at all. I have no idea what's the problem. I checked the console, but there's no erros.

我正在尝试创建我的第一个有棱角的应用。我不知道是什么问题。我检查了控制台,但是没有erros。

<head>
 <meta charset="utf-8">
 <script src="https://code.angularjs.org/1.6.0/angular.min.js"></script>
 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular-route.js"></script>
</head>
<body ng-app="myApp">
  <h1>Test angular</h1>
  <a href="#/">Main</a>
  <a href="#sec">Second</a>
  <a href="#th">Third</a>
  <div ng-view></div>
</body>

<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
  $routeProvider
  .when("/", {
      templateUrl : "main.html"
  })
  .when("/sec", {
      templateUrl : "sec.html"
  })
  .when("/th", {
      templateUrl : "th.html"
  });
});
</script>

Can anyone help me?

谁能帮我吗?

5 个解决方案

#1


27  

Routes in Angular 1.6 changed from #/myUrl to #!/myUrl

角1.6中的路由从#/myUrl更改为#!/myUrl

You should change your ref to:

你应将你的裁判更改为:

<a href="#!/">Main</a>
<a href="#!/sec">Second</a>
<a href="#!/th">Third</a>

See this answer if you want to remove this prefix.

如果要删除此前缀,请参见此答案。

#2


4  

Try adding $locationProvider to your script

尝试在脚本中添加$locationProvider

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

#3


1  

I've found out that you didn't include the $routeProvider properly, Here's the working routing code:

我发现您没有正确地包含$routeProvider,以下是工作路由代码:

app.config(['$routeProvider', function($routeProvider) {
  $routeProvider
  .when("/", {
      templateUrl : "main.html"
  })
  .when("/sec", {
      templateUrl : "sec.html"
  })
  .when("/th", {
      templateUrl : "th.html"
  });
}]);

#4


1  

Try this code it is working

试一下它正在工作的代码

app.config(['$routeProvider','$locationProvider',function ($routeProvider,$locationProvider) {
$locationProvider.hashPrefix('');
$routeProvider
    .when('/', {
        templateUrl: 'index.html'
    })
    .when('/about', {
        templateUrl: 'about.html'
    })
    .when('/service', {
        templateUrl: 'service.html'
    });}]);

#5


-3  

You need to fix your href attributes:

您需要修复href属性:

The correct way is:

正确的方法是:

<a href="#/">Main</a>
<a href="#/sec">Second</a>
<a href="#/th">Third</a>

#1


27  

Routes in Angular 1.6 changed from #/myUrl to #!/myUrl

角1.6中的路由从#/myUrl更改为#!/myUrl

You should change your ref to:

你应将你的裁判更改为:

<a href="#!/">Main</a>
<a href="#!/sec">Second</a>
<a href="#!/th">Third</a>

See this answer if you want to remove this prefix.

如果要删除此前缀,请参见此答案。

#2


4  

Try adding $locationProvider to your script

尝试在脚本中添加$locationProvider

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

#3


1  

I've found out that you didn't include the $routeProvider properly, Here's the working routing code:

我发现您没有正确地包含$routeProvider,以下是工作路由代码:

app.config(['$routeProvider', function($routeProvider) {
  $routeProvider
  .when("/", {
      templateUrl : "main.html"
  })
  .when("/sec", {
      templateUrl : "sec.html"
  })
  .when("/th", {
      templateUrl : "th.html"
  });
}]);

#4


1  

Try this code it is working

试一下它正在工作的代码

app.config(['$routeProvider','$locationProvider',function ($routeProvider,$locationProvider) {
$locationProvider.hashPrefix('');
$routeProvider
    .when('/', {
        templateUrl: 'index.html'
    })
    .when('/about', {
        templateUrl: 'about.html'
    })
    .when('/service', {
        templateUrl: 'service.html'
    });}]);

#5


-3  

You need to fix your href attributes:

您需要修复href属性:

The correct way is:

正确的方法是:

<a href="#/">Main</a>
<a href="#/sec">Second</a>
<a href="#/th">Third</a>