加载中...

参数定义


9.2. 参数定义

在作路由定义时,可以匹配一个规则,规则中可以定义路径中的某些部分作为参数之用,然后使用 $routeParams 服务获取到指定参数。比如 /#/book/test 中, test 作为参数传入到 controller 中:

  1. <div ng-view></div>
  2. <script type="text/javascript">
  3. angular.module('ngView', [],
  4. function($routeProvider){
  5. $routeProvider.when('/book/:title',
  6. {
  7. template: '{{ title }}',
  8. controller: function($scope, $routeParams){
  9. $scope.title = $routeParams.title;
  10. }
  11. }
  12. );
  13. }
  14. );
  15. </script>

访问: /#/book/test

不需要预定义模式,也可以像普通 GET 请求那样获取到相关参数:

  1. angular.module('ngView', [],
  2. function($routeProvider){
  3. $routeProvider.when('/book',
  4. {
  5. template: '{{ title }}',
  6. controller: function($scope, $routeParams){
  7. $scope.title = $routeParams.title;
  8. }
  9. }
  10. );
  11. }
  12. );

访问: /#/book?title=test


还没有评论.