How to dynamically write $index as part of ng-model name? - javascript

I am trying to use $index in an ng-repeat to dynamically add an index to my ng-model. I can't figure out how to type it so it shows up right.
This is the closest I've gotten, except the single quotes still show up:
<tr ng-model="arrayOfArrays" ng-repeat="x in exes">
<th>{{x}}</th>
<td ng-model="arrayOfArrays['{{$index}}'][0]">{{}}</td>
<td ng-model="arrayOfArrays['{{$index}}'][1]">{{}}</td>
<td ng-model="arrayOfArrays['{{$index}}'][2]">{{}}</td>
</tr>
What I want the end result to be:
<tr ng-repeat="x in exes">
<th>{{x}}</th>
<td ng-model="arrayOfArrays[0][0]">{{}}</td>
<td ng-model="arrayOfArrays[1][1]">{{}}</td>
<td ng-model="arrayOfArrays[2][2]">{{}}</td>
</tr>
I just want to know how to write the ng-model name so that the incrementing value of $index is included as part of the ng-model name. The only way I am getting the value to appear is by using extra single quotes that appear to become part of the ng-model name.

Couldn't you use
ng-repeat="(index, x) in exes"
Then index would be incremented with each "x" you go through. Also, you could use :
ng-repeat="x in exes track by $index"
This should increment $index as you currently have.

Related

How do I display an array / object using ng-repeat

I have an array that looks like the following:
I want to display that using angularJS ng-repeat, showing both count and value for each.
This is currently the HTML and angular I have but nothing displays.
I also don't need to display the 0 index in the array, I want to start from 1 if possible.
<tr ng-class="" ng-repeat="x in vm.data | filter: searchArray">
<td>{{x.value}}</td>
<td>{{x.count}}</td>
</tr>
I went back to the basics and understood that an ng-repeat is just a loop in javascript, and what do you need to do if you want to access data in JavaScript if your array is multi-dimensional? well you do a nested for loop, and thats exactly what I'm doing in this case.
This is the updated html/angularjs that displays the data I need:
<tbody>
<tr ng-repeat="x in vm.data">
<td ng-repeat="y in x">{{y.value}} {{y.count}}</td>
</tr>
</tbody>
Thank you guys again!

angular-translate multiple keys in one element

In my application I am pulling translations keys from a single cell in a database table and displaying them dynamically in a setting page. Most will only have one key in the display object but there will be a number of entries that would have multiple.
Is there a way in angular-translate to have one element host multiple keys and translate them?
en.json={"title1":"balance","title2":"other stuff"}
ctrl.list=[{display:"title1 title2"}]
<tbody class="validation-table-body">
<tr ng-repeat="entry in ctrl.list">
<td class="validation-name" >
{{entry.display | translate}}
</td>
</tr>
</tbody>
displayed:
<td>title1 title2</td>
expected:
<td>balance other stuff</td>
I am just trying to get this to work before trying to add a comma or something to display.
This is possible in the translate directive at least:
<ANY translate="{{toBeInterpolated}}"></ANY>
toBeInterpolated is the name of the key to be looked up.
Ref:https://angular-translate.github.io/docs/#/guide/05_using-translate-directive

calculation with $index in angularjs?

I want to do some arithmetic operation with $index in ng-repeat.
The following code is not working:
<tr ng-repeat="item in quotation.items track by $index">
<td class="text-center"><strong>{{$index++}}</strong></td>
<td>{{item.item}}</td>
<td>{{item.quantity}}</td>
<td>{{item.rate}}</td>
<td>{{item.rate * item.quantity}}</td>
</tr>
How can I solve this?
You need to use + 1. ++ always modifies the variable, and that never works well when you do that on a loop variable.
$index + 1
And this is the correct syntax for the ng-repeat. You don't need a by $index. $index is created automatically.
<tr ng-repeat="item in quotation.items">

ng-if only deletes innerHTML in my table

I am making a custom directive which will paginate and make my table sortable. I am trying to make it so that multiple data types will work with the sorting with no effort from the user. Here is my problematic html in my templateUrl:
<tr ng-repeat="row in cndPaginatedObject track by $index">
<td ng-repeat="data in row track by $index" ng-if="!isValidDate(row[cndTableHeaders[$index]])">
{{row[cndTableHeaders[$index]]}}
</td>
<td ng-repeat="data in row track by $index" ng-if="isValidDate(row[cndTableHeaders[$index]])">
{{row[cndTableHeaders[$index]] | date: cndDateFormat}}
</td>
</tr>
I would prefer to use ng-if as opposed to ng-show/hide because it would unnecessarily duplicate DOM elements with ng-show/hide and just not show them. I have tried both ways, but I get the same result either way. The content of columns with dates in them don't show up, but the td itself is still there taking up space. So, I end up having three headers for columns and more than three columns. As far as I was aware, ng-if is supposed to remove the entire element from the DOM.
here is my other code of significance:
scope.isValidDate = (data) => {
var timestamp = Date.parse(data);
return !(isNaN(timestamp));
}
As for cndPaginatedObject, it is just an array of objects with multiple strings contained within. Thank you for any help in advance!
UPDATE:
Just thought I would also add that the cndTableHeaders is literally the table headers. cndPaginatedObject uses the table headers as the keys to each of the values. This way they show up in the proper order in the table.
Two ways to do what you're doing better without the need to produce extra doms and looping a ng-repeat twice
A better way to do what you want to do is
<tr ng-repeat="row in cndPaginatedObject track by $index">
<td ng-repeat="data in row track by $index">
{{(!isValidDate(row[cndTableHeaders[$index]])) ? row[cndTableHeaders[$index]] : row[cndTableHeaders[$index]] | date: cndDateFormat}}
</td>
</tr>
Or add a span inside the with the ng-if logic
<tr ng-repeat="row in cndPaginatedObject track by $index">
<td ng-repeat="data in row track by $index"">
<span ng-if="!isValidDate(row[cndTableHeaders[$index]])>
{{row[cndTableHeaders[$index]]}}
</span>
<span ng-if="isValidDate(row[cndTableHeaders[$index]])">
{{row[cndTableHeaders[$index]] | date: cndDateFormat}}
</span>
</td>
</tr>
Alternatively you could create your own filter which checks if date is valid and then using the date filter to filter it.

AngularJS - ng-class with value from ng-repeat

I'm trying to set the class of a tr using ng-class with the value of ng-repeat. All the examples I see online seem to call a function. Can i just set the ng-class with a value rather than using a function?
None of these work.
<tr ng-class="{{res.resopnse.label}}" ng-repeat="res in responses">
<tr ng-class="res.resopnse.label" ng-repeat="res in responses">
<tr ng-class="{res.resopnse.label}" ng-repeat="res in responses">
If you have no conditional logic, you can just interpolate into class attribute directly:
<tr class="{{res.resopnse.label}}" ng-repeat="res in responses">

Categories