角度路由在url中使用参数时导致无限循环

时间:2017-06-26 16:00:31

标签: javascript html angularjs

尝试以角度访问参数化网址时,我得到无限循环。

这是代码。

  

app.js

var express = require("express");
var cors = require("cors");

var app = express();


app.use(express.static(__dirname));

app.use('/',function(req,res){

res.sendFile("./home.html");
});


app.listen("4467", function() {

    console.log("Started listening at port 4467 new");
});
  

home.html的

<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>

  <script src="a02.js"></script>

</head>
<body ng-app="testApp">
Hello Homepage
<div ng-view></div>
</body>
</html>
  

a02.js

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

app.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider) {

  $routeProvider.when('/test/:a', {
    templateUrl: './sum.html',
    controller: 'sum'
  })
  .when("/",{
    templateUrl:"./a02.html"
  })
  .otherwise({
    template:"Otherwise"
  });

  $locationProvider.hashPrefix('');
  $locationProvider.html5Mode({
    enabled: true,
    requireBase: false
  });
}]);

app.controller('sum', function($scope, $routeParams) {
  console.log("I am controller "+ $routeParams.a);
  $scope.a = $routeParams.a;
  $scope.b = $routeParams.b;
});
  

a02.html

Hello there
  <hr>
  <script>  console.log("Getting called again");</script>
  <a href="test/10">Click Me </a>

我的要求是当我点击Click Me链接时,它应该将现有的ng-view模板,即a02.html替换为sum.html。

  

sum.html

<div>

 Value of a: {{a}} </br>
 Value of b: {{b}}

</div>

但是当我点击Click Me链接时,我会得到无限循环。有人能告诉我这里缺少什么吗?

0 个答案:

没有答案