Skip to content

Instantly share code, notes, and snippets.

@luisxkimo
Created May 9, 2014 11:37
Show Gist options
  • Select an option

  • Save luisxkimo/65c80ff8db77967ebe89 to your computer and use it in GitHub Desktop.

Select an option

Save luisxkimo/65c80ff8db77967ebe89 to your computer and use it in GitHub Desktop.
Prueba con ng-options de Angular para una lista de opciones con valores y labels personalizados
//1 - Definimos los valores y los labels para el DropDownList y creamos una propiedad con uno de esos valores por defecto:
// ... todo dentro del controlador
$scope.orderPropList = [{
value : 'age', label : 'Newest'
}, {
value : 'name', label : 'Alphabetical'
}, {
value : '-age', label : 'Oldest'
}];
$scope.order = $scope.orderPropList[0];
//2 - En la vista, enlazamos el 'select' de la siguiente manera:
<select ng-model="order" ng-options="order.label for order in orderPropList">
</select>
//3 - Ya podemos utilizar el valor seleccionado para hacer ordenaciones
<ul class="phones">
<li ng-repeat="phone in phones | orderBy:order.value">
<span>{{phone.name}}</span>
<p>{{phone.snippet}}</p>
</li>
</ul>
@luisxkimo
Copy link
Author

Para testear los valores por defecto y que el número de opciones es el correcto:


it('should be the numbers of options equal to 3', function() {
      expect(scope.orderPropList.length).toBe(3);
    });

it('should set the default value of ordermodel', function() {
      expect(scope.order['value']).toBe('age');
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment