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?

Embattled Swag
  • 1,479
  • 12
  • 23
  • 1
    why do you need a hidden input in the first place when working with angular? – charlietfl Oct 22 '15 at 16:37
  • @charlietfl I'm trying to grab the value from the server. Not really sure how to do it otherwise. – Embattled Swag Oct 22 '15 at 17:13
  • 1
    would normally make a `$http` request and pass response to controller – charlietfl Oct 22 '15 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?](http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background) – Grundy Oct 22 '15 at 17:36
  • sidenote: attribute value always is string. so for getting js object, you need parse this string. – Grundy Oct 22 '15 at 17:37

1 Answers1

2

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

$scope.states = JSON.parse(jQuery('#states').val());
LostMyGlasses
  • 3,074
  • 20
  • 28