Legion Legion - 6 months ago 14
AngularJS Question

How do I reference programmatically generated $scope variables in my template?

I have an array fetched from the server from which I generate a variable number of boolean flags and attach them to my

$scope
variable in my controller.

for (var i = 0; i < myArray.length; i++) {
$scope['TeamId' + myArray[i].TeamId + 'IsVisible'] = ViewMatrixService['TeamId' + myArray[i].TeamId + 'IsVisible'];
}


How do I reference these variables in my template? I tried the below, but it doesn't work.

<th ng-show="'TeamId' + doc.DocumentOwnerTeam.TeamId + 'IsVisible'">

Answer

Just put them in a scope variable that you can explicitly reference.

$scope.team = $scope.team || {};
for (var i = 0; i < myArray.length; i++) {
    $scope.team['TeamId' + myArray[i].TeamId + 'IsVisible'] = ViewMatrixService['TeamId' + myArray[i].TeamId + 'IsVisible'];
}

<th ng-show="team['TeamId' + doc.DocumentOwnerTeam.TeamId + 'IsVisible']">

Though as an alternate route, if it's always going to be determined by whatever iteration of doc.DocumentOwnerTeam is currently active, just assign it as a property to that.