0

So for instance I might have a stage as described:

.state('room', {
    url: "/room/:id",
    templateUrl: "app/views/room.html",
    controller: 'roomsCtrl'

})

And then a controller as defined:

angular.module('app')
    .controller('roomsCtrl', ['$scope','Room', function ($scope,Room) {
        $scope.rooms = Room.get({id: 1}, function(item) {

        });
        console.log($scope);



    }]);

In this instance Room is a service object that has been hooked up to a restful api. I would like to test to see if id is present within the controller and pass that to the id parameter "Room.get({id: 1}" if it exists otherwise pass nothing.

How would one retrieve the parameter id which would be present within the url?

In my case it shows up like: /#/room/1

Here is an example of some html from my view with a link that will key up to the id:

<a ui-sref="room({ id: '1' })">
    {{room.name}}
</a>
2
  • Checkout $location service on Angular docs.angularjs.org/api/ng/service/$location Commented May 20, 2014 at 5:45
  • Will do, working on another project will finish that and try this. The first part didn't work. I think because of conflicts with the ui-router Commented May 21, 2014 at 6:21

1 Answer 1

2

You can use $stateParams if you are using $stateProvider

$stateParams is an angular service which allows you to retrieve the current set of state parameters.

angular.module('app')
    .controller('roomsCtrl', ['$scope','Room','$stateParams' function ($scope,Room, $stateParams) {

       var id = $stateParams.id;
       :
    }]);

If you are using $routeProvider then use $routeParams

Also Try

var id = $routeParams.id;
Sign up to request clarification or add additional context in comments.

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.