3

Im new in Angular and I stared working in one project witch is using it for front end side. The thing is. I have a dropdown witch has values 1,2,3 etc... and when you select something in dropdown, depending on what you click. currentEntry variable is changing value and im filtering data value="{{entry['properties'][currentEntry]['password']}}". This works perfectly when I have simple input tag.

But when I do this:

<input type="password" name="pasword" ng-model="password" ng-change="addProperty(password,'password')" class="form-control" placeholder="Password" value="{{entry['properties'][currentEntry]['password']}}">

whats happening up there is that ,value properity is changing when i do inspect element in code but not on the client.

Than i realized that value is stored in ng-model, so I need to somehow create this models dinamicaly for example when I click on drop down item witch has value 1 ng-model should look like this ng-model="password1" etc...

I have this number stored in variable "currentEntry" so i tried to do something like this

<input type="password" name="pasword" ng-model="password{{currentEntry}}"......>

but I get syntax error

Token '{' is an unexpected token at column 9 of the expression [password{{currentEntry}}] starting at [{{currentEntry}}].

How to solve this?

NicolasMoise
  • 7,261
  • 10
  • 44
  • 65
user3237500
  • 45
  • 2
  • 7

1 Answers1

3

You can't do dynamic variables like that in Javascript (this is not php). Instead use a function or an object.

Function

$scope.password = function(){
    //use $scope.currentEntry here
    return value;
}

and in your view

<input type="text" ng-model="password()"/>

Object

Another possibility would be this

<input type="text" ng-model="passwords[currentEntry]">

Where $scope.passwords is an object containing your passwords as such.

$scope.password= {1: 'a password', 2: 'another password'}

Also see Use dynamic variable names in JavaScript for more on dynamic variables in Javascript.

Community
  • 1
  • 1
NicolasMoise
  • 7,261
  • 10
  • 44
  • 65
  • Problem is that Will have like 70 or more input, and sometimes there can be more or less, so I think this is not the best for me, isnt there just any whey to just ad number dinamicaly on the end of the model name? I really feel so without solution – user3237500 Mar 12 '14 at 20:04
  • There's many different ways. The point is you have access to the number (`$scope.currentEntry`) inside your function, which is all you need, as far as I can tell, to get the right value. How or what you do with it afterwards is really outside the scope of this question. – NicolasMoise Mar 12 '14 at 20:08
  • There's nothing you can do in the angular view that you can't do inside that function. – NicolasMoise Mar 12 '14 at 20:10
  • Maybe this will help http://stackoverflow.com/questions/5117127/javascript-dynamic-variable-name – NicolasMoise Mar 12 '14 at 20:13
  • You said "There's nothing you can do in the angular view that you can't do inside that function." can I then put eval in html tag to parse that ? – user3237500 Mar 12 '14 at 20:22
  • No I don't think so and I wouldn't recommend it. This kind of logic should go in a service or controller. You have to think declaratively in your view. – NicolasMoise Mar 12 '14 at 20:25
  • Only conclusion I have here is that Angular is shit. I have to to simple stuff like concatenation and I have to make tons on logic to make this simple thing to work. – user3237500 Mar 12 '14 at 20:27
  • Seems like you're having a Javascript problem really. The way you try to call a dynamic variable is wrong regardless of Angular. – NicolasMoise Mar 12 '14 at 20:28
  • Ok, then tell me whats the right way if just that is the problem. I tried this ng-model="password{{currentEntry}}" , this ng-model="password[currentEntry]" – user3237500 Mar 12 '14 at 20:32
  • I don't know enough about your business logic to say which is the right way. There's different ways to do it, you have to figure one that works for you. Again check the question I linked to and my edit. – NicolasMoise Mar 12 '14 at 20:37
  • I dont have any specific busines logic, I only have function that change currentEntry $scope.changeCurrent = function(selectedEntry) { $scope.currentEntry = selectedEntry; //$("#regForm")[0].reset(); } I JUST WANT TO ADD THIS currentEntry to ng-model="something{{currentEntry}}" is it possible that its so complicated to do this in angular ?? – user3237500 Mar 12 '14 at 20:42
  • Once again, you can't do dynamic var names like that in Javascript, use objects instead. CHECK OUT THE LINKED QUESTION. – NicolasMoise Mar 12 '14 at 21:15