2016-08-01 28 views
0

私は、URLを通じてfirst.htmlからsecond.htmlに2つのパラメータを送信し、routeParamsを使用してsecond.htmlの値を取得しようとしています。私は<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; 
    }) 
}()); 

答えて

0

アプリを展開している場合、このコードhere をチェックルートコンテキスト(例:https://myapp.com/)に追加するには、ベースURL /:

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

アプリをサブコンテキスト(例: https://myapp.com/subapp/)、ベースURLをサブコンテキストのURLに設定します。

関連する問題