0

I'm very new to jQuery and its uses, but I'm trying to do something simple...I have a hidden input that contains a list of strings and I'm trying to put it into an AngularJS controller.

here's my input

<input type="hidden" id="states" value="{!states}" />

and code from my controller

$scope.states = jQuery('#states').val();

yet when I try something like alert($scope.states[0]) the alert box only contains "[", as if the first element in the states array is [, second element is A, third element is L, etc.

am I doing something wrong?

5
  • 1
    why do you need a hidden input in the first place when working with angular? Commented Oct 22, 2015 at 16:37
  • @charlietfl I'm trying to grab the value from the server. Not really sure how to do it otherwise. Commented Oct 22, 2015 at 17:13
  • 1
    would normally make a $http request and pass response to controller Commented Oct 22, 2015 at 17:16
  • @EmbattledSwag, usually, if you use jquery inside angular controlller - you do something wrong :-) try see this post “Thinking in AngularJS” if I have a jQuery background? Commented Oct 22, 2015 at 17:36
  • sidenote: attribute value always is string. so for getting js object, you need parse this string. Commented Oct 22, 2015 at 17:37

1 Answer 1

2

You need to transform the value from a string into an array. Try with JSON.parse():

$scope.states = JSON.parse(jQuery('#states').val());
Sign up to request clarification or add additional context in comments.

1 Comment

this helped, but I also had to serialize the object on the server side too. Thanks!

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.