【问题标题】:How to pass param in routeProvider in Angular JS?如何在 Angular JS 的 routeProvider 中传递参数?
【发布时间】:2015-07-12 06:31:16
【问题描述】:

我在 Angular JS 中使用 routeProvider:

.when('/profile/personal', {
       templateUrl: 'personal.html',
       controller: 'EditProfileController'
})

如何将参数传递给控制器​​EditProfileController,并在此处调用返回数据的 Ajax 方法。该数据必须显示在personal.html的路由模板中。

例子:

    .controller('EditProfileController', ['$scope', '$http', function ($scope, $http) {
         // If was click on `/profile/personal` from route, must get patam `personal` and call method GetAjax(param);
         $scope.GetAjax = function (param){
            //here return data put it in template HTML from route
         }  

    }]);

我的链接按路径位于页面中:

http://wd.tk/profile

当我点击链接路由时,我得到 URL:

http://wd.tk/profile#/profile/personal/1

我做:

.controller('EditProfileController', ['$scope', '$http', function ($scope, $http, $routeParams) {
   console.log($routeParams);
}]);

我得到错误:

TypeError: Cannot read property 'idProfile' of undefined

【问题讨论】:

  • 这个param 来自哪里?是来自路由Url吗?
  • 如果另有说明,当我点击路由中的链接时,我必须在控制器中调用 AJAX 并且响应的数据必须在路由的模板中
  • 你可以用"/profile/personal/:param"在路由url中指定一个参数,param可以通过可注入的$routeParams获得,所以,在控制器中你可以做$routeParams.param

标签: angularjs


【解决方案1】:

您需要更改您的 $routeProvider,它应该有 /profile/:type 而不是 /profile/personal,这意味着您将为 :type 提供值,可以通过在控制器内注入 $routeParams 服务来访问。

.when('/profile/:type', {
       templateUrl: 'personal.html',
       controller: 'EditProfileController'
})

控制器

.controller('EditProfileController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
     $scope.profileType = $routeParams.type;
     $scope.GetAjax = function (){
        //here you have profile type in $scope.profileType
        //you can pass it to ajax by simply accessing scope variable.
        $http.get('/getprofiledata?type='+ $scope.profileType)
        .success(function(data){
           $scope.profileData = data;
        })
        .error(function(error){
           console.log('error occured')
        })
     }  
     $scope.GetAjax(); //calling it on controller init
}]);

更新

你错过了数组注释中的一个依赖$routeParams

.controller('EditProfileController', ['$scope', '$http', '$routeParams',function ($scope, $http, $routeParams) {
   console.log($routeParams);
}]);

【讨论】:

  • 如果我需要将when 设置为:.when('/profile/contacts/', { templateUrl: 'personal.html', controller: 'EditProfileController' })
  • 那么你需要在$routeProvider.when('/profile/:type', { templateUrl: 'personal.html', controller: 'EditProfileController' }).when('/profile/contacts/', { templateUrl: 'personal.html', controller: 'EditProfileController' })这样的条件下再添加一个
  • 你能再看看我的问题并编辑自己的答案吗?因为现在有很多时刻
  • 它工作得很好,我可以检查是否存在$routeParams.type = 1
  • @Hamed 你能投票赞成并接受我的回答吗..谢谢:)
【解决方案2】:

首先,在你的url配置中,你必须把url的参数:

when('/profile/personal/:idProfile', {
       templateUrl: 'personal.html',
       controller: 'EditProfileController'
})

现在,在您的 EditProfileController 中,您应该获取参数并调用 ajax 请求:

注入 $routeParams 对象并用

获取参数
$routeParams.idProfile:

.controller('EditProfileController',  
function ($scope, $http, $routeParams) {

   var idProfile = $routeParams.idProfile;

   $http.get("service url").then(setData, setError);

   function setData(data) {
       $scope.data = data;
   }
   function setError() {
       alert("error on get data profile");
   }

}]);

在您的 html 中,您将以正确的格式显示您的数据配置文件。

但我建议所有的 ajax 调用都应该在 Angular 服务中分组。 PD: 查看 Angular ui 路由器:

What is the difference between angular-route and angular-ui-router?

希望这会有所帮助。

【讨论】:

  • 请稍等,/profile/personal/:idProfile 的链接必须是<a href="/profile/personal/1">Link</a>
  • 所以,但是如何检查是否点击了personal 然后请求 Ajax。现在您在控制器中的代码始终运行
  • 我也收到错误:TypeError: Cannot read property 'idProfile' of undefined
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多