Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Parameters in Route Urls in AngularJS

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'
                   })
like image 257
Muhammad Nasir Avatar asked Sep 02 '15 11:09

Muhammad Nasir


3 Answers

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

like image 61
suvroc Avatar answered Oct 26 '22 01:10

suvroc


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

Check the docs.

like image 37
U r s u s Avatar answered Oct 26 '22 02:10

U r s u s


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

like image 31
gaurav bhavsar Avatar answered Oct 26 '22 02:10

gaurav bhavsar