javascript - Ionic framework not loading ng-model variable - Stack Overflow

I'm building an Ionic app and I have an issue reading the value of a ng-model.HTML:<select id=&

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 0
Add a ment  | 

2 Answers 2

Reset to default 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./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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745434874a4627563.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信