Rafael Munoz
Rafael Munoz

Reputation: 656

angularJS $stateParams fail to pass a value to a controller

I have an state declared like this in my main App:

var app = angular.module("contactManagement",
        ["'ui.router'])
    .config(["$stateProvider", "$urlRouterProvider", "$locationProvider",
           function ($stateProvider, $urlRouterProvider, $locationProvider) {

    $stateProvider.state('contacts.detail', {
                url: "/contacts/:contactId",
                templateUrl: 'contacts.detail.html',
                controller: contactEditCtrl
                }
        ...

and I am trying to pass the parameter "contactId" from the URL to the Controller in another file called contactEditCtrl.js like this:

.module("contactManagement ")
.controller("ContactEditCtrl", ["contactResource", ContactEditCtrl]);

    function ContactEditCtrl(contactResource, $stateParams) {
             var vm = this;
             var contactId = $stateParams.contactId;
            vm.title =  '';
            vm.message = '';

            contactResource.get({ id: **contactId** },
            ...more stuff...

But the contactId does not reach the controller using the $stateParams as it said the documentation.

All I need is to inject that ContactId in the controller, but I do not get it!

Any idea on what I am missing here?

Upvotes: 1

Views: 127

Answers (1)

Nikola Hristov
Nikola Hristov

Reputation: 736

You forgot "params"

.state('contacts.detail', {
    url: "/contacts/:contactId",
    params:{contactId: null},
..more code...
})

Upvotes: 1

Related Questions