我正在构建一个示例应用(请参阅plunk https://plnkr.co/edit/vDXcSPrOjw5qvBQKcYvw?p=preview)。
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js"></script>
<script src="https://cdn.rawgit.com/mattiash/angular-tablesort/master/js/angular-tablesort.js"></script>
<link rel="stylesheet" href="https://cdn.rawgit.com/mattiash/angular-tablesort/master/tablesort.css">
<script>
var myApp = angular.module('myApp', ['tableSort'])
.controller("tableTestCtrl", function tableTestCtrl($scope) {
$scope.items = [{
"Id": "01",
"Name": "A",
"80th_time": "1.00",
"median_time": "1"
}, {
"Id": "02",
"Name": "B",
"80th_time": "10.00",
"median_time": "1"
}, {
"Id": "04",
"Name": "C",
"80th_time": "9.50",
"median_time": "10"
}, {
"Id": "03",
"Name": "a",
"80th_time": "9.00",
"median_time": "2"
}, {
"Id": "06",
"Name": "b",
"80th_time": "100.00",
"median_time": "2"
}, {
"Id": "05",
"Name": "c",
"80th_time": "1.20",
"median_time": "2"
}];
});
</script>
</head>
<body>
<div ng-controller="tableTestCtrl">
<table border="1" ts-wrapper>
<thead>
<tr>
<th ts-criteria="Id">Id</th>
<th ts-criteria="Name|lowercase" ts-default>Name</th>
<th ts-criteria="80th_time|parseFloat">80th Per Time</th>
<th ts-criteria="median_time|parseFloat">Median Time</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items track by item.Id" ts-repeat>
<td>{{ item.Id }}</td>
<td>{{ item.Name }}</td>
<td>{{ item.80th_time }}</td>
<td>{{ item.median_time }}</td>
</tr>
</tbody>
</table>
</div>
</body>
当我将属性的名称从“80th_time”更改为“th_time”时,角度解析起作用。如果我将属性的名称保留为“80th_time”,我会得到以下$ parse异常:
https://docs.angularjs.org/error/ $解析/语法P0 = th_time&安培; P1 =是%20AN%20unexpected%20token&安培; P2 = 3及P3 = 80th_time%7CparseFloat&安培; P4 = th_time%7CparseFloat
有关为何发生这种情况的任何想法?
为了对此进行测试(即触发异常),您可以运行plunk,然后单击“每次80次”标题(以触发基于此列的排序)。
答案 0 :(得分:1)
使用符号item.80th_time
时,您只能使用有效的variable names。要使用以数字开头的,您需要以item['80th_time']
。
答案 1 :(得分:1)
来自MDN:
对象属性名称可以是任何有效的JavaScript字符串,也可以是可以转换为字符串的任何内容,包括空字符串。但是,任何不是有效JavaScript标识符的属性名称(例如,具有空格或连字符的属性名称,或以数字开头的属性名称)只能使用方括号表示法访问。