I have the following code that shows me a series of hours from 07 to 22. What I want to know is how to obtain the value of the selected hour in my controller and if it is possible to print it in an alert.
HTML view
<div ng-controller="MyCtrl">
<div class="item item-divider item-positive item-text-wrap">
HORA INGRESO
</div>
<div class="row">
<div class="col .col-50">
<div class="item item-input item-select">
<div class="input-label">HH</div>
<select ng-model="data.hih" ng-init="data.hih=horas[0]" ng-options="hih.hora for hih in horas">
</select>
</div>
</div>
</div>
<a class="button button-block icon-left ion-ios-compose button-balanced"
ng-click="crearTutoria()">
Crear Tutoría
</a>
</div>
Controller
var myApp = angular.module('myApp',[]);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
$scope.horas = [];
for(var i=7;i<=22;i++){
if(i<10){
$scope.iconcatenado='0'+i;
$scope.horas.push({hora:$scope.iconcatenado});
}else{
$scope.horas.push({hora:i});
}
}
$scope.data = {};
//9.1 hace referencia al data de CrearTutoria.html
$scope.crearTutoria=function(){
alert($scope.data.hih)//esto me muestra [Object:Object]
alert(JSON.stringify($scope.data.hih))// me imprime hora:07 y lo que solo quiero obtener es 07 o el cualquier valor seleccionado
}
}
You have to change your expression
ng-options
of thisto this
This can be a bit confusing to understand so I'll break it down for you.
I am using the syntax
select as label for value in array
instead oflabel for value in array
because according to the documentationThe translation of this is that it will
ng-options
always select the indicated object (you have an array of objects ) unless otherwise specified.That's when the other syntax comes into play. The different parts of
select as label for value in array
meanselect
: It is the expression that will be evaluated and assigned to ng-model when an option is selected (equivalent tovalue
the element attribute<option>
, for this type of expression the array index is assigned).label
: It is an expression that will be evaluated and displayed in each of the<option>
when you expand theselect
(the text content inside the elementoption
)value
: A variable that represents each element of the collectionarray
: The collection you are iterating overThe expression that I gave you
hih.hora as hih.hora for hih in horas
means in shortI think it's easier to understand this way. Here is a working demo