1

I'm having some troubles with arrays in the ngModel.

Imagine I have a movie databse and I want to search a movie where appears some actors:

$scope.search = {
    actors: [1, 2, 3] // actors' ID
};

I'm using $location.search. In order to send an array, I have to "parse" the model and send this:

{ 'actors[]': [1, 2, 3] };

So the url will be: search?actor%5B%5D=1&actor%5B%5D=2&actor%5B%5D=3 (I'm using PHP as backend).

The problem is when I refresh the page with that URL, I'd like to have that inputs loaded, and I have to do the "reverse", so that "actors[]" have to be "actors" because the ngModel key is "actors".

I feel that I'm overcomplicated too much and there is a better way to do this.

Any help would be appreciated.

Thx!

edit:

Plunkr (Url wrong and parsed generated)

http://plnkr.co/edit/aYawMwqRrCjN9ZBxB6Us?p=preview

4
  • you dont need []. { 'actors': [1, 2, 3] } will do Commented Dec 8, 2015 at 11:50
  • @PitchaiPazhani using $location.search the url is not generating correctly Commented Dec 8, 2015 at 11:52
  • So you want to create an array of actors (that you want to use as search criteria in your back end movie DB) and then get the response back with an array (containing movies featuring those actors) and then display it in your view (as a result of a give search)? Commented Dec 8, 2015 at 12:13
  • @ChrisHermut Yes but the result and the queries are not the problem. The problem is that I have to "parse" the data when I send to the server (actors to actors[]) and the reverse when I'm load the form with the query string and the actors ngModel input (ui-select or whatever) Commented Dec 8, 2015 at 12:27

1 Answer 1

0

So you want to parse your query string as discussed in Best way to parse a query string with AngularJS without using html5mode.

And then you can just repopulate your $scope.actors array with data coming from that parse.

Sign up to request clarification or add additional context in comments.

2 Comments

So you want to read that query string as (simplified) actors[0]=1&actors[1]=2 etc.?
Yes but ui-select won't load the selected items because de ngModel key is "actors", not "actors[]", but occurs just the opposite when submitting the form, "actors[]" has to be send in order to generate the url correctly.

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.