[英]How can I get 2 different data types from 1 ng-model in select
I got fine code in mean stack, but I want one another feature in it. 我在均值堆栈中得到了不错的代码,但是我想要其中的另一个功能。 Namely I want to get more elements from my database, while I am selecting options. 也就是说,当我选择选项时,我想从数据库中获取更多元素。
My database is NoSql: 我的数据库是NoSql:
Tir2 :id, price, xin, yin, xout, yout...etc. Tir2:id,价格,xin,yin,xout,yout等。
This is my code: 这是我的代码:
index.js index.js
<select size="10" name="ngvalueselect" ng-model="response.data" multiple>
<option ng-repeat="tir in tir2" ng-value="[tir.mapsUrl]">{{tir.id}}</option></select>
ng-src="{{response.data[0]}}"
Controller.js Controller.js
$http.get('/tir2').then(function(response){
$scope.tir2 = response.data;
var googleMapsUlr = "origin={0} destination={1}";
angular.forEach($scope.tir2, function(element, key) {
element.mapsUrl = googleMapsUlr.replace("{0}", element.xout + "," + element.yout).replace("{1}", element.xin + "," + element.yin);
});
This works perfectly for me, but I need to add some more information about selected id. 这对我来说非常理想,但是我需要添加有关所选ID的更多信息。 I came up with something like this above. 我上面想出了类似的东西。
Controller.js Controller.js
var info ="{0},{1},{2},{3}";
........
........
element.info2 = info.replace("{0}", element.dateout).replace("{1}", element.datein).replace("{2}", element.price).replace("{3}", element.kmeters);
And now I don't know how can I separately show these two elements ( info2
, mapsUrl
) by one click on select options. 现在,我不知道如何通过单击选择选项来分别显示这两个元素( info2
, mapsUrl
)。
First of all you don't need to use the ng-repeat in select, you can use ng-option directive. 首先,您不需要在select中使用ng-repeat,可以使用ng-option指令。
Your model data structure is not clear but if you want to post everything say a complete user record on selection then use like this: 您的模型数据结构尚不明确,但是如果您要发布所有内容,请在选择时说出完整的用户记录,然后使用如下所示:
ng-model ='modelname'
ng-options="user as user.name for user in users track by user.id"
When you will select an item in dropdown it will put complete user record in your model. 当您在下拉列表中选择一个项目时,它将在模型中放入完整的用户记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.