我觉得很难理解AngularJS教程;这个教程带我构建一个显示手机的应用程序。我正在第5步,想试试允许用户指定要显示多少个手机。视图看起来像这样:
<body ng-controller="PhoneListCtrl">
<div class="container-fluid">
<div class="row-fluid">
<div class="span2">
<!--Sidebar content-->
Search: <input ng-model="query">
How Many: <input ng-model="quantity">
Sort by:
<select ng-model="orderProp">
<option value="name">Alphabetical</option>
<option value="age">Newest</option>
</select>
</div>
<div class="span10">
<!--Body content-->
<ul class="phones">
<li ng-repeat="phone in phones | filter:query | orderBy:orderProp">
{{phone.name}}
<p>{{phone.snippet}}</p>
</li>
</ul>
</div>
</div>
</div>
</body>
我已经添加了这行代码,用户可以输入他们想要显示的结果数量:
How Many: <input ng-model="quantity">
这是我的控制器:
function PhoneListCtrl($scope, $http) {
$http.get('phones/phones.json').success(function(data) {
$scope.phones = data.splice(0, 'quantity');
});
$scope.orderProp = 'age';
$scope.quantity = 5;
}
重要的是这一行:
$scope.phones = data.splice(0, 'quantity');
我可以硬编码一个数字来表示应显示多少个电话。如果我输入5
,就会显示5个。我想做的就是从视图中读取该输入中的数字,并将其放入data.splice
行中。我尝试过带引号和不带引号,都没有成功。我该怎么办?
limitTo
,但提供一个“加载更多”按钮以增加限制。只有当有更多记录可用时才应显示此按钮。 - Tim Büthefilter:query
是什么? - 0x777query
来自于Search: <input ng-model="query">
。 - jusopi