4

I am using ui routers stateparams and I need to update a view. If I pass any of the fields along with the url, I can update the view but since my json entry can have multiple key value pairs, the idea of adding every key to the url of stateProvider is not a smart Idea. So my question is how do I update the view by just passing one of the fields of JSON.

I put up a plnkr demo, if you see the url, I don't want to append all the text in the url, as it looks awkward. Please tell me what is the approach to solve this. Apparently I can't add a unique id to JSON and access it as state

http://plnkr.co/edit/1ItQLvu3O1gjROXR39QN?p=preview

.state('guy', {
    parent: 'guys',
    url: "/:id/:desc",
    controller:'MyCtrl',
    templateUrl: "file1.html",
  })
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Mike
  • 3,348
  • 11
  • 45
  • 80
  • Have a look at this, it might help: http://stackoverflow.com/questions/20632255/angularjs-pass-an-object-into-a-state-using-ui-router – JanR Jun 29 '15 at 04:04

2 Answers2

2

There is a working plunker

In case I understand your issue properly:

... need to pass parameters to state, but not via url...

we can solve it by state setting: params : {}.

This could be our new 'guy2' state definition

.state('guy2', {
    parent: 'guys',
     url: "/:urlParam",
     params: { 
       urlParam: null,
       param1: null,
       param2: null,
       param3: null,
    },
    controller:'MyCtrl',
    templateUrl: "guy.html",
})

The urlParam will be part of url, while the others won't. We can call it like this:

<a ui-sref="guy2({
      urlParam: guy.name, 
      param1: guy.desc,
      param2: guy.note,
      param3: guy.info,
})">{{guy.name}}</a>

and for data.json like this:

[
    {

    "name":"Jane",
    "desc":"Jane descrition ...",
    "info":"Jane Info ...",
    "note":"Jane Note ..."
    },
    ...
]

we would see link like this:

#/route/Jane

Check it in action here

There are few links to some more details:

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
-1

Why you not add your json an unique field and unique filed add with url. if your id field is unique then id field add with url otherwise show this code.

.state('guy', { parent: 'guys', url: "/:unique_id", params : {desc:null} controller:'MyCtrl', templateUrl: "file1.html", })

Sandeep
  • 1,461
  • 13
  • 26
  • I cant add a unique id to json, and also adding params : {desc:null} wont work. – Mike Jun 29 '15 at 05:03
  • then you have another option if you work with ng-repeat. ng-repeat have $index field. $index have index of array. you use $index variable for unique id. – Sandeep Jun 29 '15 at 05:10
  • I dont need to repeat anything. ng-repeat is not an option. – Mike Jun 29 '15 at 05:19