Vignesh
Vignesh

Reputation: 1063

Angular Ui-router can't access $stateParams inside my controller

I am very beginner in Angular.js, I am using the Ui-router framework for routing. I can make it work upto where I have no parameters in the url. But now I am trying to build a detailed view of a product for which I need to pass the product id into the url.

I did it by reading the tutorials and followed all the methods. In the tutorial they used resolve to fetch the data and then load the controller but I just need to send in the parameters into the controllers directly and then fetch the data from there. My code looks like below. when I try to access the $stateParams inside the controller it is empty. I am not even sure about whether the controller is called or not. The code looks like below.

(function(){
    "use strict";
    var app = angular.module("productManagement",
                            ["common.services","ui.router"]);
    app.config(["$stateProvider","$urlRouterProvider",function($stateProvider,$urlRouterProvider)
        {
            //default
            $urlRouterProvider.otherwise("/");

            $stateProvider
                //home
                .state("home",{
                    url:"/",
                    templateUrl:"app/welcome.html"
                })
                //products
                .state("productList",{
                    url:"/products",
                    templateUrl:"app/products/productListView.html",
                    controller:"ProductController as vm"
                })
                //Edit product
                .state('ProductEdit',{
                    url:"/products/edit/:productId",
                    templateUrl:"app/products/productEdit.html",
                    controller:"ProductEditController as vm"
                })
                //product details
                .state('ProductDetails',{
                    url:"/products/:productId",
                    templateUrl:"app/products/productDetailView.html",
                    Controller:"ProductDetailController as vm"
                })
        }]
    );
}());

this is how my app.js looks like. I am having trouble on the last state, ProdcutDetails.

here is my ProductDetailController.

(function(){
    "use strict";
    angular
        .module("ProductManagement")
        .controller("ProductDetailController",
                    ["ProductResource",$stateParams,ProductDetailsController]);
        function ProductDetailsController(ProductResource,$stateParams)
        {
            var productId = $stateParams.productId;
            var ref = $this;
            ProductResource.get({productId: productId},function(data)
            {
                console.log(data);
            });
        }
}());

NOTE : I found lot of people have the same issue here https://github.com/angular-ui/ui-router/issues/136, I can't understand the solutions posted their because I am in a very beginning stage. Any explanation would be very helpful.

Upvotes: 2

Views: 3098

Answers (2)

Radim Köhler
Radim Köhler

Reputation: 123861

I created working plunker here

There is state configuration

$urlRouterProvider.otherwise('/home');

// States
$stateProvider
    //home
    .state("home",{
        url:"/",
        templateUrl:"app/welcome.html"
    })
    //products
    .state("productList",{
        url:"/products",
        templateUrl:"app/products/productListView.html",
        controller:"ProductController as vm"
    })
    //Edit product
    .state('ProductEdit',{ 
        url:"/products/edit/:productId",
        templateUrl:"app/products/productEdit.html",
        controller:"ProductEditController as vm"
    })
    //product details
    .state('ProductDetails',{
        url:"/products/:productId",
        templateUrl:"app/products/productDetailView.html",
        controller:"ProductDetailController as vm"
    })

There is a definition of above used features

.factory('ProductResource', function() {return {} ;})
.controller('ProductController', ['$scope', function($scope){
  $scope.Title = "Hello from list";
}])
.controller('ProductEditController', ['$scope', function($scope){
  $scope.Title = "Hello from edit";
}])
.run(['$rootScope', '$state', '$stateParams',
  function ($rootScope, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
}])
.controller('ProductDetailController', ProductDetailsController)

function ProductDetailsController ($scope, ProductResource, $stateParams)
{
    $scope.Title = "Hello from detail";
    var productId = $stateParams.productId;
    //var ref = $this;
    console.log(productId);
    //ProductResource.get({productId: productId},function(data)    {    });
    return this;
}
ProductDetailsController.$inject = ['$scope', 'ProductResource', '$stateParams'];

Check it here

But do you know what is the real issue? Just one line in fact, was the trouble maker. Check the original state def:

.state('ProductDetails',{
    ...
    Controller:"ProductDetailController as vm"
})

And in fact, the only important change was

.state('ProductDetails',{
    ...
    controller:"ProductDetailController as vm"
})

I.e. controller instead of Controller (capital C at the begining)

Upvotes: 1

Radim Köhler
Radim Köhler

Reputation: 123861

The params in controller definition array should be strings

["ProductResource", "$stateParams"...

This should properly help IoC to inject the $stateParams

And even better:

// the info for IoC
// the style which you will use with TypeScript === angular 2.0
ProductDetailsController.$inject = ["ProductResource", "$stateParams"];

// this will just map controller to its name, parmas are defined above
.controller("ProductDetailController", ProductDetailsController);

Upvotes: 1

Related Questions