[英]send the option object when using angularjs ng-options and ng-change
I having an array of objects which I am passing to the ng-options,the array contains objects of currencies in the next pattern: 我有一个对象数组,我传递给ng-options,该数组包含下一个模式中的货币对象:
[{
"cur_iso": "ILS",
"cur_symbol": "\u20aa",
"cur_name_he": "\u05e9\u05e7\u05dc",
"cur_rate_in_nis": "1.0000",
}, {
"cur_iso": "USD",
"cur_symbol": "$",
"cur_name_he": "\u05d3\u05d5\u05dc\u05e8",
"cur_rate_in_nis": "3.8580"
}]
I am using ng-options
to make a select from this array. 我正在使用
ng-options
从这个数组中进行选择。
<select ng-model="bindCurrencyModel" ng-init="bindCurrencyModel='ILS'"
ng-options="currency.cur_iso as currency.cur_symbol+' '+currency.cur_name_he for currency in currencies track by currency.cur_iso"
ng-change="setCurrency(currency)">
</select>
when user is changing the selection I want to update 2 fields: bindCurrencyModel with the cur_iso
value and bindRateModel
with the cur_rate_in_nis
value. 当用户更改选择时,我想更新2个字段: bindCurrencyModel与
cur_iso
值和bindRateModel
与cur_rate_in_nis
值。
so I had created the next method: 所以我创建了下一个方法:
$scope.setCurrency=function(currency){
$scope.bindRateModel=parseFloat(currency.cur_rate_in_nis);
};
the and set ng-change="setCurrency(currency)" but the problem is I am getting: 和设置ng-change =“setCurrency(货币)”但问题是我得到的:
TypeError: Cannot read property 'cur_rate_in_nis' of undefined
another strange behavior is empty line I am getting in the beginning of the select. 另一个奇怪的行为是我在选择的开头得到的空行。
to see all the code I had created a fiddle.... 看到我创造了一个小提琴的所有代码....
http://jsfiddle.net/d9esbgjf/5/ http://jsfiddle.net/d9esbgjf/5/
THANKS! 谢谢!
The short version is as follows: 简短版本如下:
Change your <select>
markup to the following: 将
<select>
标记更改为以下内容:
<select ng-model="selectedCurrency" ng-init="selectedCurrency=currencies[0]"
ng-options="currency as currency.cur_symbol+' '+currency.cur_name_he for currency in currencies track by currency.cur_iso"
ng-change="setCurrency(selectedCurrency)">
</select>
Change your $scope.setCurrency()
method to the following: 将
$scope.setCurrency()
方法更改为以下内容:
$scope.setCurrency = function (currency) {
$scope.bindRateModel = parseFloat(currency.cur_rate_in_nis);
$scope.bindCurrencyModel = currency.cur_iso;
};
The first difference is that I am binding the whole currency
object to a new selectedCurrency
scope property. 第一个区别是我将整个
currency
对象绑定到一个新的selectedCurrency
范围属性。 This gives you access to the whole object, not just the cur_iso
property. 这使您可以访问整个对象,而不仅仅是
cur_iso
属性。 This saves you having to look through the currencies
array to find the full object. 这样可以节省您查看
currencies
数组以查找完整对象的麻烦。
Then I changed the ng-change
expression to pass in this whole object to $scope.setCurrency
using the scope property that it is bound to. 然后我更改了
ng-change
表达式,使用它绑定的scope属性将整个对象传递给$scope.setCurrency
。 This allows that method access to the full object. 这允许该方法访问完整对象。
Finally, you need to set $scope.bindCurrencyModel
inside the $scope.setCurrency()
function, so that it will equal just the field that you are interested in. 最后,您需要在
$scope.setCurrency()
函数中设置$scope.bindCurrencyModel
,以便它只等于您感兴趣的字段。
You're doing some strange things here, I think you could simplify it a lot. 你在这里做了一些奇怪的事情,我想你可以简化它。
Firstly, setCurrency(currency)
will be evaluated against the scope. 首先,将根据范围评估
setCurrency(currency)
。 As you never define a field currency
on the scope then this will always be undefined. 由于您从未在范围上定义字段
currency
,因此始终未定义。
You're using a lot of extra variables to reference properties that you could just reference directly. 您正在使用许多额外的变量来引用您可以直接引用的属性。 Cleaning this up would make your life much simpler.
清理它会使你的生活更加简单。
Instead of your approach, consider this: 而不是你的方法,考虑这个:
<select
ng-model="selectedCurrency"
ng-options="currency as currency.cur_symbol + ' '
+ currency.cur_name_he for currency in currencies">
We removed ng-change
and make the selectedCurrency
the model, which references the whole currency object not just the ISO symbol. 我们删除了
ng-change
并将selectedCurrency
作为模型,该模型引用整个货币对象而不仅仅是ISO符号。
Now you can do this: 现在你可以这样做:
<input type="number"
ng-model="selectedCurrency.cur_rate_in_nis">
And {{selectedCurrency.cur_iso}}
for example would be the ISO code. 例如
{{selectedCurrency.cur_iso}}
将是ISO代码。
Finally to remove the blank line at the start of the select, initialise the value.. eg: 最后,在select的开头删除空行,初始化值。例如:
$scope.currencies = [{
"cur_iso": "ILS",
"cur_symbol": "\u20aa",
"cur_name_he": "\u05e9\u05e7\u05dc",
"cur_rate_in_nis": 1.0000,
"cur_last_update": "",
"cur_order": "1",
"cur_available": "1"
}, {
"cur_iso": "USD",
"cur_symbol": "$",
"cur_name_he": "\u05d3\u05d5\u05dc\u05e8",
"cur_rate_in_nis": 3.8580,
"cur_last_update": "2015-02-24 13:34:25",
"cur_order": "2",
"cur_available": "1"
}];
// Initialise value so select is initially selected on something.
$scope.selectedCurrency = $scope.currencies[0]
Hopefully you see that this jsfiddle is a lot neater and much easier to follow than your original approach. 希望你看到这个jsfiddle比你原来的方法更整洁,更容易遵循。
I believe your setCurrency()
method won't have access to currency
, only the usual scope variables. 我相信你的
setCurrency()
方法将无法访问currency
,只能访问通常的范围变量。 In this case the bindCurrencyModel
variable could be used. 在这种情况下,可以使用
bindCurrencyModel
变量。
Update your ng-options to read: 将ng-options更新为:
ng-options="currency as currency.cur_symbol+' '+currency.cur_name_he for currency in currencies track by currency.cur_iso"
This will populate your model with the full object you are expecting in the setCurrency function. 这将使用您在setCurrency函数中期望的完整对象填充模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.