0

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?

0

2 Answers 2

6

I 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.com/angular/angular.js/wiki/Understanding-Scopes

Sign up to request clarification or add additional context in comments.

Comments

0

The recommended 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.com/2012/12/14/nested-scopes-in-angularjs.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.