javascript - Get index of current object in array? Pass current object to a function. Angular JS -


i trying call function object in array. (the objects of array placed on map.) function should evaluate 'status' object has, , return url icon based on status. have pass current object function.. how do it?

my array of objects:

mvc.models = [{   id: "1",   icon: evaluatecolor(currentobject),   status: 'green' }, {   id: "2",   icon: evaluatecolor(currentobject),   status: 'red' }]; 

my function:

function evaluatecolor(currentobject) {   if (currentobject.status === 'green') {     return 'images/green_marker.png'   }   else if (currentobject.status === 'yellow') {     return 'images/yellow_marker.png'   }   else {     return 'images/red_marker.png'   } } 

i have tried pass object function passing 'this', object logs undefined. because 'this' refers controller , not object.

mvc.models = [{   id: "1",   icon: evaluatecolor(this),   status: 'green' }, {   id: "2",   icon: evaluatecolor(this),   status: 'red' }]; 

and if wants see html: (the objects houses should shown on angular google maps)

<ui-gmap-markers         models='mvc.models'         coords="'coords'"         icon="'icon'"         > </ui-gmap-markers> 

can point me in right direction? thanks!

you don't need pass whole object evaluatecolor. modify to

function evaluatecolor(status) {   if (status === 'green') {     return 'images/green_marker.png'   }   else if (status === 'yellow') {     return 'images/yellow_marker.png'   }   else {     return 'images/red_marker.png'   } } 

and call evaluatecolor('green'/'yellow') , anyways have color available while initializing object. this.status not work object declaration still not complete.

alternatively, can call initialize models without icon attribute , call function:

for(var in models){    models[i].icon = evaluatecolor(models[i].status); } 

hope solves query.. :)


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -