0

我正在学习网络编程和 AngularJS。我的 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 && ng-init,但我不知道在这种情况下如何使用它们..

4

2 回答 2

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 = [{}, ...];

请参阅 fiddle 以获取完整代码的工作示例:

http://jsfiddle.net/hpeinar/zwnbet95/

于 2014-08-11T11:40:15.753 回答
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 代码大大减少了。

使用这个:帮助链接

希望能帮助到你.......!

于 2014-08-11T11:02:55.077 回答