【问题标题】:How to use current url params in angular controller如何在角度控制器中使用当前 url 参数
【发布时间】:2013-07-03 13:47:06
【问题描述】:

我在 url 中有一个动态值参数,我想在加载时获取它并在控制器中使用 但是我不知道如何处理$route.current.params

假设我有这个路由提供者

$routeProvider.when('foo/:bar', {templateUrl: 'template.html'};

然后在我的控制器中

app.controller('mapCtrl', function($scope, $route, $routeParams) {

  var param = $route.current.params.bar;
  console.log(param);

});

如果用户访问foo/abcdefg 假设应该在控制台中显示 abcdefg 但我得到了错误。 '无法读取未定义的属性'参数''

【问题讨论】:

  • 你在 $routeParams.bar 中得到了什么?
  • 未定义,我想是因为我在没有触发角度的情况下输入 url 栏来访问 url。
  • 如果我直接在视图中使用{{$route.current.params.bar}},那么它可以工作。
  • 最好设置一个 plunker 或 fiddle 演示以使您的代码正常工作,因为很难说出它为什么不工作

标签: angularjs angularjs-routing


【解决方案1】:
$routeProvider.when('foo/:bar', {templateUrl: 'template.html', controller: 'mapCtrl'};
app.controller('mapCtrl', function($scope, $route, $routeParams) {

  var param = $routeParams.bar
  console.log(param);

});

【讨论】:

  • @vzhen 检查参数名称,它应该与您在$routeProvider中声明的名称完全匹配。
【解决方案2】:

例如:

在您的 app_route.js 中,您必须将 url 模式链接到特定控制器和用于显示数据的模板:

angular.module('mapApp', []).
    config(['$routeProvider', function($routeProvider){ 
                $routeProvider.when('/foo/:bar', {templateUrl: './view/partial/template.html', controller: mapCtrl});
                $routeProvider.otherwise({redirectTo: '/foo/01'});
            }
            ]);

在您的特定控制器中添加逻辑(此处按栏选择数据): 同样用于 $routeParams & $route.current.params

function mapCtrl($scope, $routeParams, $http) {
    $http.get('./data/myDatas.json').success( function(data){
                                        var arr = new Array();
                                        for(var i=0; i < data.length; i++){
                                            if(data[i].bar == $routeParams.bar){
                                                arr.push(data[i]);                              }
                                            }
                                        $scope.items = arr;
                                        });
}
 mapCtrl.$inject = ['$scope', '$routeParams', '$http']; //for minified bug issue

在你的 template.html 中,布局:

<div ng-repeat="item in items">
    <h3>{{item.bar}}</h3>
</div>

别忘了在你想要显示数据的索引页面上添加 ng-view,并在 html 标签中添加 ng-app="mapApp"。

希望对你有帮助^^

更多信息,请参阅:http://docs.angularjs.org/tutorial/step_07

【讨论】:

    【解决方案3】:

    要访问 $route.current.params 你当然需要包含 ngRoute 模块,该模块是通过包含额外的 angular-route.js 来加载的

     <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js"></script>
     <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular-route.js"></script>
    

    您可以通过创建 init() 函数或使用 routeChangeSuccess 访问 current.params onLoad。

    请参阅下面的代码和jsfiddle 上的工作示例。

     <script type="text/javascript">
    
     var app = angular.module( "myApp", ['ngRoute'] );
    
     app.config( function ( $routeProvider ) {
      $routeProvider
      .when( '/foo/:bar', {
          controller: 'MainCtrl',
          templateUrl: 'template.html' }
      );
    });
    
    app.controller('MainCtrl', [
      '$rootScope', '$scope', '$route', '$routeParams', function($rootScope, $scope, $route, $routeParams){
    
        $scope.routeParams = {};
        $scope.routeParams.bar = $routeParams.bar;
    
        var init = function () {
          $scope.initCurrentParams = {};
          $scope.$route = $route;
          $scope.initCurrentParams.bar = $scope.$route.current.params.bar;
          console.log('from init', $scope.initCurrentParams.bar);
        };
        init();
    
        $scope.$on('$routeChangeSuccess', function() {
    
          $scope.routeChangeSuccessCurrentParams = {};
          $scope.$route = $route;
          $scope.routeChangeSuccessCurrentParams.bar = $scope.$route.current.params.bar;
          console.log('from routeChangeSuccess', $scope.routeChangeSuccessCurrentParams.bar);
        });
    }]);
    
    </script>
    
    <div ng-app="myApp" class="ng-scope">
        <script type="text/ng-template" id="template.html">
          $routeParams.bar: {{ routeParams.bar }} <br />
          initCurrentParams.bar: {{ initCurrentParams.bar }} <br />
          routeChangeSuccessCurrentParams.bar: {{ routeChangeSuccessCurrentParams.bar }} <br />
        </script>
    
        <div class="ng-scope">
          <ul>
              <li><a href="#/foo/1">bar 1</a></li>
          </ul>
          <ng-view></ng-view>
        </div>
      </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      相关资源
      最近更新 更多