按控制器设置默认顺序
Setting default order by controller
有人可以帮我解决这个问题 - 可能 - 非常菜鸟的问题,拜托?
我有以下 html 代码:
<!doctype html>
<html>
<head>
<title>Starting Angular</title>
</head>
<!-- The ng-app directive triggers load and setup of AngularJS
after the DOM is loaded.
It sets the tag as the root of the AngularJS app. -->
<body ng-app="cardApp">
<!-- Identify the controller and model to be used. -->
<div ng-controller="MainController">
<!-- The ng-bind gets data from the model. -->
<h1 ng-bind="greeting"></h1>
<br />
Sort by:
<select ng-model="orderProp">
<option value="suit">Suit</option>
<option value="numOrd">Number</option>
</select>
<table>
<tr><th>Number</th><th>Suit</th></tr>
<!-- Populate the HTML with each object in the cards model. -->
<tr ng-repeat="card in cards | orderBy:orderProp ">
<td ng-bind ="card.number"></td>
<td ng-bind ="card.suit"></td>
</tr>
</table>
<br />
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script src="js/controllers.js"></script>
</body>
</html>
我想设置按数字显示的卡片的默认顺序。如何修改此控制器?
// Register controller with angular module.
var cardApp = angular.module('cardApp', []);
// The controller is a constructor function that takes a $scope parameter.
// The controller lets us establish data binding between the controller and the view.
// The model is initialized with the $scope parameter.
cardApp.controller('MainController', ['$scope', function ($scope) {$scope.val = "numOrd"
// Scope ensures that any changes to the
// model are reflected in the controller.
// Here we create an initialize a 'title' model.
$scope.greeting = "AngularJS Hello World!";
// Define cards model which stores an array of objects.
$scope.cards = [
{ "number": "2", "suit": "Hearts", "numOrd": 2 },
{ "number": "10", "suit": "Spades", "numOrd": 10 },
{ "number": "5", "suit": "Spades", "numOrd": 5 },
{ "number": "Q", "suit": "Hearts", "numOrd": 12 }
];
}]);
提前感谢!
另一种方法是使用另一个属性扩展您的模型suitOrd
这些属性为您的每套花色分配一个特定的编号。多亏了这一点,您可以按此属性进行排序,因为它将是一个数字
您可以在 Array 类中使用函数 'sort'。它将对您的卡片数组进行排序。
array.sort([compareFunction])
在这种情况下:
$scope.cards.sort(function(a, b){
return a['numOrd'] - b['numOrd'];
});
有关"排序"的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
你想做的是把它添加到你的js中。
$scope.orderProp = "numOrd";
相关文章:
- 用嵌套函数和默认函数定义函数
- ajax请求的顺序总是不同的
- 按照选项卡索引的顺序循环一个jQuery选择
- 如何使用铁流星与流星的默认路线
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- 匹配一个单词,其中候选人可以跨越顺序组(跨度)
- 按顺序添加和删除类
- CKeditor:更改对话框中的默认选择选项
- 在不阻止默认行为的情况下检测IE10中的缩放
- 按我自己的类克隆另一个元素的内容和顺序
- 如何从外部页面激活非默认引导选项卡
- 防止默认锚点行为AngularJS
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 阻止数据表上的默认顺序事件
- 按控制器设置默认顺序
- Angular:自定义事件处理程序的顺序&默认处理程序
- 如何设置角度智能表的默认排序顺序
- 如何使默认<选项>在Javascript中,同时按顺序排序
- 如何在AngularJS中指定默认排序顺序
- 浏览器将文件加载到缓存中的默认顺序是什么?