简体   繁体   English

如何从1 ng模型中选择2种不同的数据类型

[英]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. 现在,我不知道如何通过单击选择选项来分别显示这两个元素( info2mapsUrl )。

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. 当您在下拉列表中选择一个项目时,它将在模型中放入完整的用户记录。

For more detail see this link 有关更多详细信息,请参见此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为一个指令创建的多个输入字段分配不同的ng-model值 - How can I assign different ng-model values to several input fields created from one directive 如何处理绑定到同一ng模型的不同输入类型? - How to handle different input types bound to same ng-model? 控制器ng模型数据与视图上的ng模型数据不同 - Controller ng-model data different from ng-model data on the view 如何从另一个ng-controller访问另一个ng-controller ng-model? - How can I access the another ng-controller ng-model from another ng-controller? 如果值是从另一个ng-model分配的,则无法获取ng-model的值 - Can't get ng-model's value if the value is assigned from another ng-model 角度选择未从ng模型分配中获取设置 - angular select not get getting set from ng-model assignment 如何使用ng-repeat中的值来隐藏ng-model值 - How can I concatinate a ng-model value with a value from a ng-repeat 如何将ng-model中的值用于ng-show? - How can i use values from ng-model for ng-show? 如何在angularjs中的两个不同的ng-model之间绑定数据 - How to bind data between two different ng-model in angularjs 如何从下拉列表中选择与angularjs中的ng-model等效的数据 - how to select the data from dropdown list that is equivalent to the ng-model in angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM