AngularJS过滤器不能使用$ scope变量

时间:2015-11-01 22:23:05

标签: javascript angularjs filter angularjs-ng-repeat

我的AngularJS应用程序应该在表格中显示课程及其等价物。该表应该根据select元素的输入动态显示结果。

HTML

<form name = "Select University" ng-show = "courseType == 'extern'">
    <label for = "University"> University: </label>
    <select name = "University" id = "repeatSelect" ng-change = "changeIt()"ng-model = "univs.repeatSelect">
            <option ng-repeat = "univ in univs.uniNames | orderBy: 'School'" > {{univ.School}} </option>
    </select>
    </form>



 Courses at {{univs.repeatSelect}}
    <table>
            <tr>
            <th> Course </th>
            <th> Equivelance </th>
            </tr>

            <tr ng-repeat="x in names | orderBy: 'className' | filter:univs.repeatSelect">
            <td>{{ x.className }}</td>
            <td> {{x.equiv}} </td>
            </tr>
    </table>

的JavaScript

$http.get("http://linux.students.engr.scu.edu/~cmulloy/COEN174/getSchools.php")
    .success(function (response) 
    {
            $scope.univs.uniNames = response.records;
    });

$scope.univs = 
    {
            repeatSelect: null,
            uniNames: null,
    }; 

范围变量{{univs.repeatSelect}}显示在页面上。例如,它将显示加州大学洛杉矶分校和#34;在桌子上方。但是当我使用univs.repeatSelect作为ng-repeat的过滤器参数时,表格为空。但是,如果我明确使用“加州大学洛杉矶分校”和“加州大学洛杉矶分校”。作为过滤器参数,它可以工作并显示加州大学洛杉矶分校的课程。有什么我想念的吗?

1 个答案:

答案 0 :(得分:0)

您应该为选择value添加options属性,这会将value属性值分配给相应的ng-model

<select name = "University" id = "repeatSelect" ng-change="changeIt()" ng-model="univs.repeatSelect">
    <option ng-repeat="univ in univs.uniNames | orderBy: 'School'" value="{{niv.School}}"> {{univ.School}} </option>
</select>

更好的选择是在处理ng-options框时使用select

<select name="University" id="repeatSelect" 
   ng-change="changeIt()" ng-model="univs.repeatSelect" 
   ng-options="univ.School as univ.School in univs.uniNames | orderBy: 'School'">
</select>