<base href =“”/>导致“[$ rootScope:infdig]”错误

时间:2016-08-01 11:24:21

标签: javascript angularjs

我正在尝试通过网址从first.htmlsecond.html发送两个参数,并使用second.html获取routeParams中的值。我已将基本标记设置为<base href="file:///D:/abhilash/node/angapp/"/>,单击按钮时,输入文本框中的参数应通过url传递。

first.html:

   <!DOCTYPE html>
<html ng-app="myapp">
<head>
    <title></title>
    <base href="file:///D:/abhilash/node/angapp/"/>

</head>
<body>
<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript" src="angular-route.js"></script>
<script type="text/javascript" src="controller.js"></script>

<div ng-controller="firstController">
    First name:<input type="text" ng-model="firstName"><br>
    Last name:<input type="" ng-model="lastName"><br>
    <input type="button" ng-click="loadView()" value="submit" name="">

</div>
</body>
</html>

Second.html:

    <!DOCTYPE html>
<html ng-app="myapp">
<head>
    <title></title>
<base href="file:///D:/abhilash/node/angapp/"/>
</head>
<body>

<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript" src="angular-route.js"></script>
<script type="text/javascript" src="controller.js"></script>

<div ng-controller="secondController">
 {{firstName}}

</div>

</body>
</html>

这是控制器:

(function(){
var app = angular.module('myapp', ['ngRoute']);

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

    $routeProvider.when('/first',{
        templateUrl:'/first.html',
        controller: 'firstController'
    })
    .when('/second/:firstName/:lastName',{
        templateUrl:'/second.html',
        controller:'secondController'
    })
    .otherwise({
        redirectTo:'/first'
    })
    $locationProvider.html5Mode(true);
})



app.controller('firstController',function($scope,$location){
        $scope.firstName="";
        $scope.lastName="";
        $scope.loadView = function()
        {
            $location.path('second/'+$scope.firstName +"/" +$scope.lastName);
            console.log($location.url());
        }
    })
    .controller('secondController',function($scope,$routeParams){
        $scope.firstName = $routeParams.firstName;
        $scope.lastName = $routeParams.lastName;
    })
}());

1 个答案:

答案 0 :(得分:0)

检查此代码here 如果您要将应用部署到根上下文(例如https://myapp.com/),请将基本网址设置为/:

<head>
  <base href="/">
  ...
</head>

如果要将应用部署到子上下文(例如https://myapp.com/subapp/),请将基本URL设置为子上下文的URL:

<head>
  <base href="/subapp/">
  ...
</head>