I don’t understand if you really want to create an array or an object. In the example you quoted you are creating an object. In javascript you can access object properties by name.
var my_obj = {'teste':{'name':'Teste 123'},'lorem':{'name':'Lorem Ipsum'}};
That would be a valid notation in your angular code:
{{ my_obj['lorem']['name'] }}
But this is more semantic and facilitates reading:
{{ my_obj.lorem.name }}
It is still possible to use both forms so:
{{ my_obj['lorem'].name }}
Maybe you’re confusing it with an array because of this (I think it’s what you’re looking for).
To declare an array you would do so:
var my_array = [{'name' : 'Teste 123' }, { 'name' : 'Lorem Ipsum' }];
But the arrays can only be accessed by the index:
{{ my_array[0].name }} //acessando o primeiro elemento
To declare a simple list of strings, you could avoid the property name
:
var my_array = ['Teste 123', 'Lorem Ipsum'];
Accessing:
{{ my_array[0] }} //acessando o primeiro elemento
Finally, if you want to create a simple list of strings that can be accessed by an indexer name, the solution would be to create an object yes, almost your initial idea, but a little simpler. Thus:
var my_obj = { 'teste': 'Teste 123', 'lorem': 'Lorem Ipsum' };
Accessing:
{{ my_obj['lorem'] }} ou {{ my_obj.lorem }}
What do you expect
{{ my_array }}
exhibit? Maybe you want to{{ my_array.length }}
.– Oeslei
Javascript arrays are different from objects, arrays are declared as
my_array = []
and objects such asmy_array = {}
. Arrays have only numeric indices. To view the object you can use the native angular json filter{{ my_array | json }}
– Eduardo Lelis