最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年05月22日 10:16:41   作者:justforuse  
本篇文章主要介紹了詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

AngularJS 路由 是通過(guò) # + 標(biāo)記 幫助我們區(qū)分不同的邏輯頁(yè)面并將不同的頁(yè)面綁定到對(duì)應(yīng)的控制器上。因此在設(shè)置好路由規(guī)則后,為html頁(yè)面的a標(biāo)簽設(shè)置href路由鏈接切換不同的視圖。Angular1.6版本之前通常有href=“#...”或href=“#/...”這兩種寫(xiě)法,結(jié)果這兩種寫(xiě)法在Angular1.6中沒(méi)有任何反應(yīng)。

結(jié)果查看了下瀏覽器地址欄,默認(rèn)視圖鏈接竟然顯示“#!/..”,是的,中間多加了個(gè)!號(hào)。

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

解決方案一:

所以在html頁(yè)面a標(biāo)簽上將href的屬性值添加一個(gè)!號(hào)就可以了。

<p><a href="#!/addStudent" rel="external nofollow" rel="external nofollow" >添加學(xué)生</a></p> 
<p><a href="#!/viewStudents" rel="external nofollow" rel="external nofollow" >查看學(xué)生</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" >添加學(xué)生</a></p> 
      <p><a href="#!/viewStudents" rel="external nofollow" rel="external nofollow" >查看學(xué)生</a></p> 
      <div ng-view></div> 
      <script type="text/ng-template" id="addStudent.html"> 
        <h2>添加學(xué)生</h2> 
        {{message}} 
      </script> 
      <script type="text/ng-template" id="viewStudents.html"> 
        <h2>查看學(xué)生</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="這個(gè)頁(yè)面是用于顯示學(xué)生信息的輸入表單"; 
      }); 
      mainApp.controller("ViewStudentsController",function($scope){ 
        $scope.message="這個(gè)頁(yè)面是用于顯示所有學(xué)生信息"; 
      }); 
    </script> 
  </body> 
</html> 

解決方案二:

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

mainApp.config(["$locationProvider","$routeProvider",function($locationProvider,$routeProvider){ 
    $locationProvider.hashPrefix(''); 
}]); 
<p><a href="#addStudent" rel="external nofollow" rel="external nofollow" >添加學(xué)生</a></p> 
<p><a href="#viewStudents" rel="external nofollow" rel="external nofollow" >查看學(xué)生</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" >添加學(xué)生</a></p> 
      <p><a href="#viewStudents" rel="external nofollow" rel="external nofollow" >查看學(xué)生</a></p> 
      <div ng-view></div> 
      <script type="text/ng-template" id="addStudent.html"> 
        <h2>添加學(xué)生</h2> 
        {{message}} 
      </script> 
      <script type="text/ng-template" id="viewStudents.html"> 
        <h2>查看學(xué)生</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="這個(gè)頁(yè)面是用于顯示學(xué)生信息的輸入表單"; 
      }); 
      mainApp.controller("ViewStudentsController",function($scope){ 
        $scope.message="這個(gè)頁(yè)面是用于顯示所有學(xué)生信息"; 
      }); 
    </script> 
  </body> 
</html> 


這樣瀏覽器訪問(wèn)時(shí),就不會(huì)多出個(gè)!號(hào)了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

台安县| 自治县| 蓬安县| 舟曲县| 分宜县| 延津县| 辽宁省| 肇源县| 响水县| 仁怀市| 富源县| 康定县| 江北区| 南丹县| 高雄市| 清徐县| 昌宁县| 乐昌市| 顺昌县| 化德县| 娄底市| 霍邱县| 嫩江县| 交口县| 芒康县| 普格县| 岢岚县| 台山市| 岳普湖县| 苗栗县| 罗田县| 湖州市| 个旧市| 淮北市| 老河口市| 衡水市| 富顺县| 安康市| 湘阴县| 曲松县| 海城市|