Noobish Noobish - 1 month ago 15
AngularJS Question

Passing data attributes with ui-sref

I want pass some extra data into the controller from

ui-sref
. I am looping a json object and adding data to the sref:
<a data-imgName="{{ img.name }}" data-imgDesc="{{ img.desc }}" ui-sref="main.imgDesc({imgid: '{{ img.id }}'})">


$stateProvider.state('main.imgDesc', {
url: '/image/:imgid',
templateUrl: '...',
controller: 'ImageDescCtrl'
});


I want
imgName
and
imgDesc
into the controller. I have checked
$state
but its not found in it. Is there any way?

Answer

We should use params : {} to declare any kind of parameter, even complex object:

$stateProvider.state('main.imgDesc', {
    url: '/image/:imgid',
    templateUrl: '...',
    controller: 'ImageDescCtrl',
    params: {
       imageData: null,
    }
});

And we are ready to pass those

ui-sref="main.imgDesc({imgid: img.id, imageData: img })"

and controller can ask for $stateParams where both will be available (if passed as above)