I'm building an Ionic app and I have an issue reading the value of a ng-model
.
HTML:
<select id="objetivos"
class="form-control col-xs-12 col-sm-12 col-lg-12"
ng-model="objetivosSelec"
ng-options="objetivo.value for objetivo in objetivos | filter: filtroServicio | orderBy: 'value' | unique: 'value'"
ng-change="">
<option value=""></option>
</select>
Controller:
.controller('TecnicasCtrl', function($scope, Tecnicas, Servicios,Objetivos,Indicaciones,Contraindicaciones) {
...
$scope.limpiarFiltro = function() {
alert($scope.objetivosSelec);
}
...
})
I just want to show on an alert the selected value of the select but nothing is displayed. If I add .val()
then a JavaScript error is arised:
*Error: $scope.objetivosSelec is undefined*
I'm reading the Angular and Ionic doc and everything seems ok.
Has anyone an idea of what I'm doing wrong?
I'm building an Ionic app and I have an issue reading the value of a ng-model
.
HTML:
<select id="objetivos"
class="form-control col-xs-12 col-sm-12 col-lg-12"
ng-model="objetivosSelec"
ng-options="objetivo.value for objetivo in objetivos | filter: filtroServicio | orderBy: 'value' | unique: 'value'"
ng-change="">
<option value=""></option>
</select>
Controller:
.controller('TecnicasCtrl', function($scope, Tecnicas, Servicios,Objetivos,Indicaciones,Contraindicaciones) {
...
$scope.limpiarFiltro = function() {
alert($scope.objetivosSelec);
}
...
})
I just want to show on an alert the selected value of the select but nothing is displayed. If I add .val()
then a JavaScript error is arised:
*Error: $scope.objetivosSelec is undefined*
I'm reading the Angular and Ionic doc and everything seems ok.
Has anyone an idea of what I'm doing wrong?
Share Improve this question edited Sep 9, 2014 at 20:29 ROMANIA_engineer 56.8k30 gold badges210 silver badges205 bronze badges asked Sep 9, 2014 at 20:22 Daniel Garcia JonesDaniel Garcia Jones 811 silver badge4 bronze badges 02 Answers
Reset to default 6I finally got it!!
HTML:
ng-model="$parent.servicioSelec"
I needed to use $parent in my ng-model items to read them from the controller.
This link was very useful to solve this:
https://github./angular/angular.js/wiki/Understanding-Scopes
The remended method is to always use an object as the model and not a single value. This error happens because angular creates nested scopes for the child directives in you view and the way prototypal inheritance works. Please refer http://jimhoskins./2012/12/14/nested-scopes-in-angularjs.html