ng-init="x.y=5" works but ng-init="x[0]=5" does not?

68 views
Skip to first unread message

Matt Kruse

unread,
Sep 3, 2013, 2:11:08 AM9/3/13
to ang...@googlegroups.com
I'm working on some directives that convert old forms into Angular forms, so I convert this:
  <input name="age" value="5">
to:
  <input ng-model="age" ng-init="age=5">

It works well in most cases, and even in 'object' cases:
  <input ng-model="person.age" ng-init="person.age=5">

But fails when input names are arrays with [] notation:
  <input ng-model="person[0].age" ng-init="person[0].age=5">

Can ng-init not handle dynamically creating arrays, as it seems to do with objects?
If I do this instead:
  <input ng-model="person[0].age" ng-init="person=[];person[0]={};person[0].age=5">

then it works fine. But I can't really do this kind of initialization because then person[1] will reset the person[] array.

Any thoughts?

Matt Kruse

Sander Elias

unread,
Sep 3, 2013, 2:36:28 AM9/3/13
to ang...@googlegroups.com
Hi Matt,

I'm not sure what your goal is using array's? 
assuming you have an fixed form, with multiple persons in it (I guess?)
you could do something like:
<form .... ng-init="persons=[{},{},{}];person0=persons[0];person1=persons[1].... >

Dynamically creating array's like you are trying does not make sense in a normal angular app. In a situation like this you would do something like:

<form...ng-init="persons=[{},{},{},{}]...>
  <div ng-repeat="person in persons">
      <input ng-model="person.age ....>

I get the use for it in your special situation though!

Regards
Sander

Matt Kruse

unread,
Sep 3, 2013, 8:56:41 AM9/3/13
to ang...@googlegroups.com
On Tuesday, September 3, 2013 1:36:28 AM UTC-5, Sander Elias wrote:
I'm not sure what your goal is using array's? 

I am working with existing server-side code that generates array syntax.
 
<form .... ng-init="persons=[{},{},{}];person0=persons[0];person1=persons[1].... >

Because this is generated inside a directive (same one as my other post) I cannot do the initialization in the form. I need to only process each input element.
 
Dynamically creating array's like you are trying does not make sense in a normal angular app.

Definitely not. My purpose is to add Angular to an existing app and get a bunch of its functionality (like validation) without having to modify the app's code at all. At least not yet.
You can see a full example of what I am working on here: http://jsfiddle.net/FhmgT/3/

Matt

Sander Elias

unread,
Sep 3, 2013, 10:09:53 AM9/3/13
to ang...@googlegroups.com
Hi Matt,

Your own directive can update the <form ....> tag that init's the persons array. 
It's a bit much to create a sample for this, but it's not really that hard!
I assume you also created an directive for the form tag?
use the post-link off that tag to add the array-init to the tag.
Can you follow my reasoning? Does this make a bit sense to you?

I do like your approach to this. I don't have much time to spare, but if I can help you a bit, just drop me a note.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages