I have been provided a JSON file for an app I am helping to create.
Unfortunately it is a bit all over the place. Instead of being consistent
in it's layout I have for e.g.
- actions:
[
-
{
- type: "SHOT",
- value: "YES"
},
-
{
- type: "PLAYER",
- value: "GERRARD"
},
-
{
- type: "PASSES",
- value: "5"
}
],
then the next one is something like
- actions:
[
-
{
- type: "ACTION-TYPE",
- value: "CROSS"
},
-
{
- type: "PLAYER",
- value: "GERRARD"
},
- {
- type: "OTHER PLAYER",
- value: "ROONEY"
},
],
So as you can see, i'm dealing with different 'type' strings.
I thought it would be best to create a for loop in my controller to loop
through each keyword and pull out and transform the data I needed with some
if/else statements (a sample below):
$scope.actionType = function(event) {
$scope.timeArray = [];
$scope.passes = '';
$scope.action = '';
$scope.player = '';
for (var j = 0; j < event.actions.length; j++){
var obj = event.actions[j];
if (obj.type === 'PASSES'){
$scope.passes = obj.value;
}
if (obj.type === 'ACTION-TYPE'){
if (obj.value === 'OFFSIDE'){
$scope.action = 'Offside';
}
else if (obj.value === 'Cross'){
$scope.action = 'Cross!';
}
else{
$scope.action = obj.value;
}
}
return: true;
};
and so on.
In the front end, i access this inside an ng-repeat and then call an ng-if
(it was the only i could think of to pass the event to the function) and
bring out the values
<li ng-repeat="event in events.events">
<div ng-if="actionType(event)" >
{{passes}}
{{action}}
{{player | lowercase}}
</div>
</li>
So, firstly - I have no idea if this is a truly terrible and long winded
way of dealing (and accessing) this kind of data.
Secondly, i cannot figure out how to access the stored data in each indexed
instance.
For example I am about to add a click function which opens up some
different information but i want to bring the relevant {{passes}}
{{action}} {{player}} over as a title for a deeper breakdown.. i have tried
so many different ways but i can only ever access the last information of
the repeat (which makes sense as that is what the variable has stored).
Basically I think I need a bit of advice/guidance to understand this a bit
more.
Thanks in advance!
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.