• No se han encontrado resultados

2.1 MARCO TEÓRICO

2.1.2 CICLO HIDROLÓGICO

2.1.2.3 Proceso de escorrentía superficial

AngularJS provides an ngOptions directive to populate the <select> elements in your application. Although this is at first glance a trivial matter, ngOptions utilizes a convoluted comprehension_expression that can populate the dropdown from a data object in a variety of ways.

Getting ready

Assume that your application is as follows:

(app.js)

angular.module('myApp', [])

.controller('Ctrl', function($scope) { $scope.players = [

number: 17, name: 'Alshon', position: 'WR' }, { number: 15, name: 'Brandon', position: 'WR' }, { number: 22, name: 'Matt', position: 'RB' }, { number: 83, name: 'Martellus', position: 'TE' }, { number: 6, name: 'Jay', position: 'QB' } ]; $scope.team = { '3B': { number: 9, name: 'Brandon' }, '2B': { number: 19, name: 'Marco' }, '3B': { number: 48, name: 'Pablo' }, 'C': { number: 28, name: 'Buster' }, 'SS': {

number: 35, name: 'Brandon' } }; });

How to do it…

The ngOptions directive allows you to populate a <select> element with both an array and an object's attributes.

Populating with an array

The comprehension expression lets you define how you want to map the data array to a set of <option> tags and its string label and corresponding values. The easier implementation is to only define the label string, in which case the application will default to set the <option> value to the entire array element, as follows:

(index.html)

<div ng-app="myApp">

<div ng-controller="Ctrl">

<!—- label for value in array -—> <select ng-model="player"

ng-options="p.name for p in players"> </select>

</div> </div>

This will compile into the following (with the form CSS classes stripped):

<select ng-model="player"

ng-options="player.name for player in players"> <option value="?" selected="selected"></option> <option value="0">Alshon</option> <option value="1">Brandon</option> <option value="2">Matt</option> <option value="3">Martellus</option> <option value="4">Jay</option> </select> JSFiddle: http://jsfiddle.net/msfrisbie/vy62c575/

Here, the values of each option are the array indices of the corresponding element. As the model it is attached to is not initialized to any of the present elements, AngularJS inserts a temporary null value into the list until a selection is made, at which point the empty value will be stripped out. When a selection is made, the player model will be assigned to the entire object at that array index.

Explicitly defining the option values

If you don't want to have the <option> HTML value assigned the array index, you can override this with a track by clause, as follows:

(index.html)

<div ng-app="myApp">

<div ng-controller="Ctrl">

<!—- label for value in array -—> <select ng-model="player"

ng-options="p.name for p in players track by p.number"> </select>

</div> </div>

This will compile into the following:

<select ng-model="player"

ng-options="p.name for p in players track by p.number"> <option value="?" selected="selected"></option>

<option value="17">Alshon</option> <option value="15">Brandon</option> <option value="22">Matt</option> <option value="83">Martellus</option> <option value="6">Jay</option> </select> JSFiddle: http://jsfiddle.net/msfrisbie/umehb407/

Explicitly defining the option model assignment

If instead you wanted to explicitly control the value of each <option> element and force it to be the number attribute of each array element, you can do the following:

(index.html)

<div ng-app="myApp">

<div ng-controller="Ctrl">

<!—- label for value in array -—> <select ng-model="player"

ng-options="p.number as p.name for p in players"> </select>

</div> </div>

This will compile into the following (with the form CSS classes stripped):

<select ng-model="player"

ng-options="p.number as p.name for p in players"> <option value="?" selected="selected"></option>

<option value="17">Alshon</option> <option value="15">Brandon</option> <option value="22">Matt</option> <option value="83">Martellus</option> <option value="6">Jay</option> </select> JSFiddle: http://jsfiddle.net/msfrisbie/jtsz46cp/ However, now when an <option> element is selected, the player model will only be assigned the number attribute of the corresponding object.

Implementing option groups

If you want to take advantage of the grouping abilities for the <select> elements, you can add a group by clause, as follows:

(index.html)

<div ng-app="myApp">

<div ng-controller="Ctrl">

<!—- label for value in array -—> <select ng-model="player"

ng-options="p.name group by p.position for p in players">

</select> </div> </div>

This will compile to the following:

<select ng-model="player"

ng-options="p.name group by p.position for p in players"> <option value="?" selected="selected"></option>

<optgroup label="WR"> <option value="0">Alshon</option> <option value="1">Brandon</option> </optgroup> <optgroup label="RB"> <option value="2">Matt</option> </optgroup> <optgroup label="TE"> <option value="3">Martellus</option> </optgroup> <optgroup label="QB"> <option value="4">Jay</option> </optgroup> </select> JSFiddle: http://jsfiddle.net/msfrisbie/2d6mdt9m/ Null options

If you want to allow a null option, you can explicitly define one inside your <select> tag, as follows:

(index.html)

<select ng-model="player" ng-options="comprehension_expression"> <option value="">Choose a player</option>

Populating with an object

The <select> elements that use ngOptions can also be populated from an object's attributes. It functions similarly to how you would process a data array; the only difference being that you must define how the key-value pairs in the object will be used to generate the list of <option> elements. For a simple utilization to map the value object's number property to the entire value object, you can do the following:

(index.html)

<div ng-app="myApp">

<div ng-controller="Ctrl">

<!—- label for value in array -—> <select ng-model="player"

ng-options="p.number for (pos, p) in team"> </select>

</div> </div>

This will compile into the following:

<select ng-model="player"

ng-options="p.number for (pos, p) in team"> <option value="?" selected="selected"></option> <option value="1B">9</option> <option value="2B">19</option> <option value="3B">48</option> <option value="C">28</option> <option value="SS">35</option> </select> JSFiddle: http://jsfiddle.net/msfrisbie/zofojs7n/

The <option> values default to the key string, but the player model assignment will still be assigned the entire object that the key refers to.

Explicitly defining option values

If you don't want to have the <option> HTML value assigned the property key, you can override this with a select as clause:

(index.html)

<div ng-app="myApp">

<select ng-model="player"

ng-options="p.number as p.name for (pos, p) in team"> </select>

</div> </div>

This will compile into the following:

<select ng-model="player"

ng-options="p.number as p.name for (pos, p) in team"> <option value="?" selected="selected"></option>

<option value="1B">Brandon</option> <option value="2B">Marco</option> <option value="3B">Pablo</option> <option value="C">Buster</option> <option value="SS">Brandon</option> </select> JSFiddle: http://jsfiddle.net/msfrisbie/ssLzvtaf/

Now, when an <option> element is selected, the player model will only be assigned the number property of the corresponding object.

How it works…

The ngOptions directive simply breaks apart the enumerable entity it is passed, into digestible pieces that can be converted into <option> tags.

There's more…

Inside a <select> tag, ngOptions is heavily preferred to ngRepeat for performance reasons. Data binding isn't as necessary in the case of dropdown values, so an ngRepeat implementation for a dropdown that must watch many values in the collection adds unnecessary data binding overhead to the application.

Documento similar