2016-02-15 5 views
1

Может кто-то, пожалуйста, помогите мне с этим - возможно, очень нуб вопрос, пожалуйста?Настройка по умолчанию по контроллеру

У меня есть следующий 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 } 
    ]; 
}]); 

Заранее благодарен!

+0

Вы хотите изменить убывающую форму (по возрастанию)? – vitozev

+0

Покажите свою функцию 'orderProp' – suvroc

+0

ученик BCIT, я угадываю? – Julix

ответ

0

Другой метод заключается в расширении вашей модели с другими свойствами suitOrd задающих каждый из вашего костюма один конкретный номер , Благодаря этому вы можете сортировать по этой собственности, потому что это будет номер

+0

Я предполагаю, что вы имеете в виду numOrd, не так ли? потому что я не вижу suitOrd в любом месте, и он делает то, о чем вы говорите. - Но тогда вы просто говорите «сортировать по этому свойству» - не зная, как это сделать, именно здесь проблема. Просто замените orderProp на numOrd или что? – Julix

0

Что вы хотите сделать, это добавить это в свой js.

$scope.orderProp = "numOrd"; 
Смежные вопросы