如何从路径中获取变量?

时间:2021-10-27 23:18:00

I have this path:

我有这条路径:

http://localhost:1234/#/Plya/Home

Sometimes the path above can have variable:

有时,上面的路径可以有变量:

http://localhost:1234/#/Plya/Home/1234

My question is how to get variable 1234 from the path using angularJS?

我的问题是如何使用angularJS从路径中获取变量1234 ?

2 个解决方案

#1


1  

You must use in your controller $routeParams that contains the variable:

您必须在控制器中使用包含变量的$routeParams:

$scope.id = $routeParams.userName;

In the routing you have to specify:

在路由中你必须指定:

app.config(function ($routeProvider) {
    $routeProvider.when('/configUser/:userName', {
            templateUrl: '/app/views/configUser.html',
            controller: 'configUserController'
        });
});

#2


1  

You need $routeParams.

你需要routeParams美元。

When defining your route:

在定义你的路线:

$routeProvider
    .when('/Plya/Home/:plyaId', {
      templateUrl: 'plya.html',
      controller: 'PlyaController'
    });

In PlyaController:

在PlyaController:

module.controller('PlyaController',['$routeParams',function($routeParams) {
  $scope.plyaId = $routeParams.plyaId;
}]);

#1


1  

You must use in your controller $routeParams that contains the variable:

您必须在控制器中使用包含变量的$routeParams:

$scope.id = $routeParams.userName;

In the routing you have to specify:

在路由中你必须指定:

app.config(function ($routeProvider) {
    $routeProvider.when('/configUser/:userName', {
            templateUrl: '/app/views/configUser.html',
            controller: 'configUserController'
        });
});

#2


1  

You need $routeParams.

你需要routeParams美元。

When defining your route:

在定义你的路线:

$routeProvider
    .when('/Plya/Home/:plyaId', {
      templateUrl: 'plya.html',
      controller: 'PlyaController'
    });

In PlyaController:

在PlyaController:

module.controller('PlyaController',['$routeParams',function($routeParams) {
  $scope.plyaId = $routeParams.plyaId;
}]);