Link to home
Start Free TrialLog in
Avatar of kashif Riaz
kashif RiazFlag for Pakistan

asked on

sending parameters with url in angularjs code when using ng-route

I am creating a shopping list application. without going to deep , here I will try to make my code as short as possible . I have a table in my database named "listsnames". in this table, there are just names of lists created by me (or other users).
these lists are shown on page as below
<div ng-app = "myapp"  ng-controller = "myCtrl">
           <ul ng-repeat="x in list">
          <li>{{x.listName}}</li> 
          </ul>
</div>
Angular Code:
var app = angular.module("myapp",[]);
app.controller("myCtrl",function($scope,$http) {
$http.get(
"serversidepage.php"
{'data':'useremail'}
).then(function(response) {
$scope.lists = response.data
})
});

Open in new window

Now all lists names are shown. and it is time that when  i click on any list name , the link should be redirect to  a page where all items of this list are there Or where new items will be inserted to that list.
But problem is that how i will send an id or other parameter with url of that list  to targeted page where the targeted page will show data according to the parameters sent with url.e.g.  if a list has id "101" then it will be sent to the targeted page and targeted page will fetch data from database where id = "101".
If this problem is solved then my applications is almost complete.Please help me. I have tried my best searching on google but  i could not understand the $routeparams service of AngularJs. actually  I know $routParams service a bit , but not know where to put the actual value of a parameter which is coming from database
Avatar of Rahul Goel
Rahul Goel
Flag of India image

You can use the :id and here is sample to implement the same.

$routeProvider.when('details/:id', {
     templateUrl: 'partials/details.html',
     controller: 'DetailController'
});

myApp.controller('DetailController', ['$scope', '$routeParams', function($scope, $routeParams){
     $scope.message = "Your ID is " + $routeParams.id;
}]);

Open in new window


But I will recommend to use ui-router in angular as it is vast and has multilevel routing.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.