国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法

2019-11-19 16:31:50
字體:
來源:轉載
供稿:網友

AngularJS 路由 是通過 # + 標記 幫助我們區分不同的邏輯頁面并將不同的頁面綁定到對應的控制器上。因此在設置好路由規則后,為html頁面的a標簽設置href路由鏈接切換不同的視圖。Angular1.6版本之前通常有href=“#...”或href=“#/...”這兩種寫法,結果這兩種寫法在Angular1.6中沒有任何反應。

結果查看了下瀏覽器地址欄,默認視圖鏈接竟然顯示“#!/..”,是的,中間多加了個!號。

AngularJS升級到了1.6版本后,里面多了很多/#!/的改動。那么1.6究竟做了哪些改變呢?可以參考這個:https://github.com/angular/angular.js/commit/aa077e81129c740041438688dff2e8d20c3d7b52

解決方案一:

所以在html頁面a標簽上將href的屬性值添加一個!號就可以了。

<p><a href="#!/addStudent" rel="external nofollow" rel="external nofollow" >添加學生</a></p> <p><a href="#!/viewStudents" rel="external nofollow" rel="external nofollow" >查看學生</a></p> 

完整代碼:

<html>   <head>     <meta charset="utf-8" />     <title>AngularJS 視圖</title>     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.min.js"></script>   </head>   <body>     <h2>AngularJS 視圖</h2>     <div ng-app="mainApp">       <p><a href="#!/addStudent" rel="external nofollow" rel="external nofollow" >添加學生</a></p>       <p><a href="#!/viewStudents" rel="external nofollow" rel="external nofollow" >查看學生</a></p>       <div ng-view></div>       <script type="text/ng-template" id="addStudent.html">         <h2>添加學生</h2>         {{message}}       </script>       <script type="text/ng-template" id="viewStudents.html">         <h2>查看學生</h2>         {{message}}       </script>     </div>     <script>       var mainApp=angular.module("mainApp",['ngRoute']);       mainApp.config(["$routeProvider",function($routeProvider){         $routeProvider.when('/addStudent',{           templateUrl:'addStudent.html',           controller:'AddStudentController'         }).when('/viewStudents',{           templateUrl:'viewStudents.html',           controller:'ViewStudentsController'         }).otherwise({           redirectTo:'/addStudent'         });       }]);       mainApp.controller("AddStudentController",function($scope){         $scope.message="這個頁面是用于顯示學生信息的輸入表單";       });       mainApp.controller("ViewStudentsController",function($scope){         $scope.message="這個頁面是用于顯示所有學生信息";       });     </script>   </body> </html> 

解決方案二:

如果想讓路由依舊表現的與之前版本的一致可以這樣做:

mainApp.config(["$locationProvider","$routeProvider",function($locationProvider,$routeProvider){     $locationProvider.hashPrefix(''); }]); 
<p><a href="#addStudent" rel="external nofollow" rel="external nofollow" >添加學生</a></p> <p><a href="#viewStudents" rel="external nofollow" rel="external nofollow" >查看學生</a></p> 

完整代碼:

<html>   <head>     <meta charset="utf-8" />     <title>AngularJS 視圖</title>     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.min.js"></script>   </head>   <body>     <h2>AngularJS 視圖</h2>     <div ng-app="mainApp">       <p><a href="#addStudent" rel="external nofollow" rel="external nofollow" >添加學生</a></p>       <p><a href="#viewStudents" rel="external nofollow" rel="external nofollow" >查看學生</a></p>       <div ng-view></div>       <script type="text/ng-template" id="addStudent.html">         <h2>添加學生</h2>         {{message}}       </script>       <script type="text/ng-template" id="viewStudents.html">         <h2>查看學生</h2>         {{message}}       </script>     </div>     <script>       var mainApp=angular.module("mainApp",['ngRoute']);       mainApp.config(["$locationProvider","$routeProvider",function($locationProvider,$routeProvider){         $locationProvider.hashPrefix('');         $routeProvider.when('/addStudent',{           templateUrl:'addStudent.html',           controller:'AddStudentController'         }).when('/viewStudents',{           templateUrl:'viewStudents.html',           controller:'ViewStudentsController'         }).otherwise({           redirectTo:'/addStudent'         });       }]);       mainApp.controller("AddStudentController",function($scope){         $scope.message="這個頁面是用于顯示學生信息的輸入表單";       });       mainApp.controller("ViewStudentsController",function($scope){         $scope.message="這個頁面是用于顯示所有學生信息";       });     </script>   </body> </html> 

這樣瀏覽器訪問時,就不會多出個!號了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临颍县| 兴安盟| 新营市| 汝城县| 漯河市| 三穗县| 鹰潭市| 栾城县| 新泰市| 西林县| 桑日县| 克什克腾旗| 长春市| 金溪县| 云南省| 加查县| 武威市| 津市市| 久治县| 永顺县| 永安市| 合作市| 铜鼓县| 大同市| 平阴县| 富川| 滕州市| 陕西省| 博兴县| 汶川县| 伊通| 天祝| 乌鲁木齐县| 鄱阳县| 绥棱县| 白河县| 葫芦岛市| 盐津县| 九龙县| 东阳市| 阿坝县|