5

How can we pass parameters to partial view in angularJs. I am new to angular ,I am following These tutorials for learning .This tutorial explain basic quite well but nothing about how can we send parameter to partial view. e.g /addStudent?id=45

 $routeProvider.
                   when('/addStudent', {
                      templateUrl: 'addStudent.htm',
                      controller: 'AddStudentController'
                   }).
                   when('/viewStudents', {
                      templateUrl: 'viewStudents.htm',
                      controller: 'ViewStudentsController'
                   })

4 Answers 4

4

You can define parameter in address definition and access it by $stateParams controller parameter

  when(url: '/new/:portfolioId',
    templateUrl: 'new.html',
    controller: function($scope, $stateParams) {
      $scope.portfolioId = $stateParams.portfolioId;
    }
  )

Look here: http://benfoster.io/blog/ui-router-optional-parameters

Sign up to request clarification or add additional context in comments.

Comments

3

The more "Angular way" to achieve that is by using the $routeParams service.

Check the docs.

Comments

2

to pass parameter in route you can define parameter as :parameter in you url

like :

$routeProvider.
               when('/addStudent', {
                  templateUrl: 'addStudent.htm',
                  controller: 'AddStudentController'
               }).
               when('/viewStudents', {
                  templateUrl: 'viewStudents.htm',
                  controller: 'ViewStudentsController'
               }).
               when('/students/:id', {
                  templateUrl: 'studentsDetail.htm',
                  controller: 'studentsDetailController'
               })

here url students/:id has id as a parameter.

Here is angular doc

Comments

2
$routeProvider.when('/addStudent/:id', {
                 templateUrl: 'addStudent.htm',
                 controller: 'AddStudentController'
                   })

$routeProvider.when('/viewStudents/:id', {
                 templateUrl: 'viewStudents.htm',
                 controller: 'ViewStudentsController'
                   })

3 Comments

Explain why this is a correct answer else it will be deleted.
The URLs in the when() calls now define a parameter. It is the part starting from the colon (:param) AngularJS will now extract from the URL (route path) whatever comes after the #/route1/ part.
Code-only answers, while they are permitted, they don't tend to get a whole lot of upvotes. Try explaining why the code works and what problems it addresses and how. I disagree with Mamta about it being deleted, but it's just not as useful as an answer that's well documented. Just my $0.02.

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.