离子选择中的奇怪行为,ng-model 不会更新 [英] Weird behavior ionic in select, ng-model won't update

查看:28
本文介绍了离子选择中的奇怪行为,ng-model 不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些奇怪的事情,这个例子在 codepen 中有效,但在我的 Ionic 应用中无效.

I'm experiencing something weird, this example works in codepen but won't work in my Ionic app.

当我更改 select 标签中的选项时,我想显示所选值,但它不起作用,它显示 undefined,我尝试了很多方法.

When I change the option in the select tag I want to show the selected value, but it won't work, it shows undefined, i've tried in many ways.

这不是原始代码,原始代码从外部 API 检索值并使用 ngOptions 填充选项(它可以工作,可以填充).但它不会更新控制器中的值.

This is not the original code, the original one retrieves the values from an external API and populates the options with ngOptions (which works, it populates ok). But it won't update the value in the controller.

所以我决定让它更简单,但它仍然不起作用:

So I decided to make it more simple, and it still won't work:

HTML

<select ng-model="optionSelected" ng-change="selectUpdated()">
    <option value="">Select an option</option>
    <option value="h">Hello</option>
    <option value="b">Bye</option>
</select>

JAVASCRIPT

$scope.selectUpdated = function() {
    console.log('Updated');
    console.log($scope.optionSelected);
};

我认为不需要更多代码,HTML 包含在 ion-viewion-content 中.未显示任何错误,仅显示已更新"输出和 undefined.

I don't think more code is needed, the HTML is contained in ion-view and ion-content. No errors are shown, only the 'Updated' output and undefined.

更改选项时,我得到 undefined.但是 codepen 中的相同代码工作得很好.. http://codepen.io/anon/pen/YXvYmq

When changing the option, I get undefined. But this same code in codepen works just fine.. http://codepen.io/anon/pen/YXvYmq

有人能告诉我是什么触发了这种奇怪的行为吗?

Can someone tell me what can be happening that triggers this odd behavior?

提前致谢.

推荐答案

找到解决方案,将 ngModel 属性作为参数传递到 ngChange 中.

Found the solution, pass the ngModel property as a parameter in the ngChange.

HTML

<select ng-model="optionSelected" ng-change="selectUpdated(optionSelected)">
    <option value="">Select an option</option>
    <option value="h">Hello</option>
    <option value="b">Bye</option>
</select>

JS

$scope.selectUpdated = function(optionSelected) {
    console.log('Updated');
    console.log(optionSelected);
};

这篇关于离子选择中的奇怪行为,ng-model 不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆