【问题标题】:How to get parameters value using $routeParams from URL in angular js?如何使用 $routeParams 从 Angular js 中的 URL 获取参数值?
【发布时间】:2017-01-18 07:05:08
【问题描述】:

我正在研究 Angularjs,我遇到了奇怪的问题,我无法使用 $routeParams 从 URL 获取参数值..

var MyApp = angular.module('testAngularApp', ['ngRoute','ngResource'])
    .config(['$routeProvider',
        function ($routeProvider) {
            $routeProvider.
                when('/', {
                when('/updateUser/:id', {
                    templateUrl: 'Pages/User/Edit.html',
                    controller: 'UpdateUserCtrl'
                })
        }]);

控制器

angular.module('testAngularApp').
    controller('UpdateUserCtrl', ['$scope', 'updateUserService', '$http','$route', '$routeParams', function ($scope, updateUserService, $routeParams,$route, $http) {
        $scope.testV = "Update User Ctrl";
        console.log("hello");
        $scope.params1 = $routeParams.id; 
        console.log($routeParams.id ); //i am getting undefined
        console.log($scope.params1); //i am getting undefined
        var check = $routeParams['id'];
        console.log(check); //i am getting undefined
    }]);

请告诉我如何从 URL 中获取值,我已经通过了这些解决方案,但它对我不起作用.. link1 link2

【问题讨论】:

    标签: javascript angularjs controller routeparams


    【解决方案1】:

    我认为services 的顺序是错误的。尝试如下更改

    controller('UpdateUserCtrl', ['$scope', 'updateUserService','$route','$http' '$routeParams',
                         function ($scope, updateUserService,$route, $http, $routeParams)
    

    【讨论】:

      【解决方案2】:
      $routeParams
      

      将从 URL 获取参数列表到您的控制器

      你定义的函数定义错误。它的定义应该与我们为局部变量创建的顺序相同。

      controller('UpdateUserCtrl', 
      ['$scope', 'updateUserService', '$http','$route','$routeParams', 
      function ($scope, updateUserService, $http,$route,$routeParams ) {
      // your Script goes Here.
      console.log($routeParams.id);
      }
      

      【讨论】:

        猜你喜欢
        • 2022-01-06
        • 1970-01-01
        • 2018-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        相关资源
        最近更新 更多