我的范围函数不在angularjs中运行

时间:2013-03-01 14:56:02

标签: angularjs

我正在尝试制作可排序的表格。我写了以下视图和控制器:

查看:

<div class="container" ng-app>
<div ng-controller="SortableTblCtrl">
    <table class="table table-hover table-bordered span12">
        <tr>
            <th ng-repeat="metadata in metadatas" ng-click="sort('{{metadata.columnProperty}}')" style="cursor: pointer">{{metadata.columnName}} </th>
        </tr>
        <tr ng-repeat="person in persons | orderBy:predicate:reverse">
            <td>{{person.firstName}}</td>
            <td>{{person.lastName}}</td>
        </tr>
    </table>
</div>

控制器:

function SortableTblCtrl($scope) {
$scope.persons = [
    { "firstName": "firstname1", "lastName": "lastname1" },
    { "firstName": "firstname2", "lastName": "lastname2" },
    { "firstName": "firstname3", "lastName": "lastname3" }
];
$scope.predicate = "";
$scope.reverse = false;

$scope.sort = function (pred) {
    $scope.predicate = pred;
    $scope.reverse = !$scope.reverse;
};

$scope.metadatas = [
    { "columnProperty": "firstName", "columnName": "First Name" },
    { "columnProperty": "lastName", "columnName": "Last Name" }
];

}

当我点击列标题时,没有任何反应。我找到了一些东西,就是当我将ng-click="sort('{{metadata.columnProperty}}')"更改为ng-click="sort('firstName')"时,当我点击每个列标题时,它会对第一列进行排序。

1 个答案:

答案 0 :(得分:1)

这:ng-click="sort('{{metadata.columnProperty}}')"应为ng-click="sort(metadata.columnProperty)"