angularjsHow do I use path variables in AngularJS?
Path variables in AngularJS are used to store parameters in the URL. These parameters can be used to pass information to the controller and can be retrieved using the $routeParams service.
Example code
var app = angular.module('myApp', ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
.when('/users/:userId', {
templateUrl: 'user.html',
controller: 'UserController'
});
});
app.controller('UserController', function($scope, $routeParams) {
$scope.userId = $routeParams.userId;
});
In the above example, we define a route with a path variable userId
and use the $routeParams
service to get the value of userId
in the controller.
Code explanation
var app = angular.module('myApp', ['ngRoute']);
: Declares a new AngularJS application.$routeProvider.when('/users/:userId', {...})
: Defines a route with a path variableuserId
.$scope.userId = $routeParams.userId;
: Retrieves the value ofuserId
using the$routeParams
service.
Helpful links
More of Angularjs
- How do I use Angular with YAML?
- How can I become an Angular expert from a beginner level?
- How can I use Angular to zoom in and out of a div?
- How do I create a yes/no dialog box using Angular?
- How can I use the Yandex Map API with AngularJS?
- How can I use Angular and Zorro together to create a software application?
- How do I install Yarn using Angular?
- How can I implement XSS protection in an AngularJS application?
- How can I use Zone.js with Angular to detect and act upon asynchronous events?
- How can I use an Angular YouTube Player in my software development project?
See more codes...