0

I am working on an app about routing, my code:

//HTML, I passed a 'test' into routing
<a href="#details/test">Test</a>
<div data-ng-view=""></div>

//Template
<h1>{{res}}</h1>

//Angularjs
var app = angular.module("dictApp", ['ngRoute']);

app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
    when('/details/:test', {
        templateUrl: 'template.html', 
        controller: 'testCtrl'
    });
}]);

app.controller('testCtrl', function ($scope, $routeProvider) {
    $scope.res = $routeProvider.test;
});

//The template is displayed as
{{res}}

The template page should display 'test', but I don't know why it didn't work.

Thansk in advance.

2 Answers 2

1

'test' parameter should be available in $routeParams.

app.controller('testCtrl', function ($scope, $routeParams) {
    $scope.res = $routeParams.test;
});
Sign up to request clarification or add additional context in comments.

Comments

1

The service that exposes the route parameters is $routeParams. $routeProvider is the provider used to configure the routes in the app like the one you have done in your code using .when method as well

You need to inject $routeParams and use it instead of $routeProvider

app.controller('testCtrl', function ($scope, $routeParams) {
  $scope.res = $routeParams.test;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.