我正在学习网页编程和AngularJS。我在HTML文件中有许多冗余代码,但我不知道如何删除它们。
HTML文件:
<select id="selectSearch" ng-model="searchValue">
<option value="7">7</option>
<option value="6">6</option>
<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select>
...
<tr>
<td></td>
<td colspan="3">Class</td>
<td x-ng-if="searchValue >= 1">{{ getClass(studentYear) }}</td>
<td x-ng-if="searchValue >= 2">{{ getClass(studentYear+1) }}</td>
<td x-ng-if="searchValue >= 3">{{ getClass(studentYear+2) }}</td>
<td x-ng-if="searchValue >= 4">{{ getClass(studentYear+3) }}</td>
<td x-ng-if="searchValue >= 5">{{ getClass(studentYear+4) }}</td>
<td x-ng-if="searchValue >= 6">{{ getClass(studentYear+5) }}</td>
<td x-ng-if="searchValue >= 7">{{ getClass(studentYear+6) }}</td>
</tr>
<tr>
<td></td>
<td colspan="3">Grades</td>
<td x-ng-if="searchValue >= 1">{{ getGrades(studentYear) }}</td>
<td x-ng-if="searchValue >= 2">{{ getGrades(studentYear+1) }}</td>
<td x-ng-if="searchValue >= 3">{{ getGrades(studentYear+2) }}</td>
<td x-ng-if="searchValue >= 4">{{ getGrades(studentYear+3) }}</td>
<td x-ng-if="searchValue >= 5">{{ getGrades(studentYear+4) }}</td>
<td x-ng-if="searchValue >= 6">{{ getGrades(studentYear+5) }}</td>
<td x-ng-if="searchValue >= 7">{{ getGrades(studentYear+6) }}</td>
</tr>
<tr>
<td></td>
<td colspan="3">Status</td>
<td x-ng-if="searchValue >= 1">{{ getStatus(studentYear) }}</td>
<td x-ng-if="searchValue >= 2">{{ getStatus(studentYear+1) }}</td>
<td x-ng-if="searchValue >= 3">{{ getStatus(studentYear+2) }}</td>
<td x-ng-if="searchValue >= 4">{{ getStatus(studentYear+3) }}</td>
<td x-ng-if="searchValue >= 5">{{ getStatus(studentYear+4) }}</td>
<td x-ng-if="searchValue >= 6">{{ getStatus(studentYear+5) }}</td>
<td x-ng-if="searchValue >= 7">{{ getStatus(studentYear+6) }}</td>
</tr>
...
我想我需要使用 ng-repeat &amp;&amp; ng-init ,但在这种情况下我不知道如何使用它们。
答案 0 :(得分:1)
由于很难真正理解表格背后的逻辑,我已经做了一个相当普遍的例子,说明如何使用ng-repeat和ng-select来播放一些数据。
请参阅以下代码:
<div data-ng-controller="nodeTodayCtrl">
<label for="selectYear">Select year you want info for</label>
<select data-ng-model="selectedYear" data-ng-options="year for year in possibleYears"></select>
<!-- we are assuming that there are in total of 9 classes and after that they've finished school -->
<table>
<tr>
<td>Name</td>
<td>Gender</td>
<td>Is in class on selected year</td>
</tr>
<tr data-ng-repeat="student in students">
<td>{{student.name}}</td>
<td>{{student.gender}}</td>
<td>{{ getClass(student, selectedYear); }}</td>
</tr>
</table>
</div>
使用Javascript:
$scope.possibleYears = [];
$scope.yearRange = 10;
// populate possible years, start from 0 to include current year
for(var i = 0; i <= $scope.yearRange;i++) {
$scope.possibleYears.push((new Date()).getFullYear() + i);
}
$scope.getClass = function(student, selectedYear) {
var currentYear = (new Date()).getFullYear();
// default to current year
if(!selectedYear) {
selectedYear = currentYear;
}
var classIn = student.class + (selectedYear - currentYear);
return (classIn > 9) ? 'Finished school '+ (classIn - 9) +' year(s) ago' : classIn;
};
// your students data
$scope.students = [{}, ...];
请参阅完整代码的工作示例小提琴:
答案 1 :(得分:0)
一个例子: 你有一个数组,如Arrayed = [{value:1},{value:2},{value:3},{value:4},{value:5}];
<select id="selectSearch" ng-model="searchValue" ng-repeat="single in Arrayed">
<option value="{{single.value}}">{{single.value}}</option>
</select>
在这里,您可以看到与您的相比,html代码大幅减少。
使用此:LINK FOR HELP
希望它有所帮助.......!