角JS - 获取选定的文本为NG模式(无NG选项) [英] Angular JS - get selected text as ng model (without ng-options)

查看:218
本文介绍了角JS - 获取选定的文本为NG模式(无NG选项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code后面,我填补了一个DropDownList如下(用VB)

In my code behind, I fill up a DropDownList as follows (using VB)

ddlCountries.DataSource = dt ' dt is DataTable with columns "CountryCode" and "CountryName"
ddlCountries.DataTextField = "CountryName"
ddlCountries.DataValueField = "CountryCode"
ddlCountries.DataBind()
ddlCountries.Attributes.Add("ng-model", "CountryName")

然后客户端,我有一个选择的标签,这是充满了来自如下服务器端选项:

Then client side, I have a select tag, which is filled with options from server side as below:

<select ng-model="CountryName">
    <option value="IN">India</option>
    <option value="US">United States</option>
    <option value="ML">Malaysia</option>
    <!-- and other 200+ records -->
</select>

我不能使用 NG-选项在这里!现在,每当我改变从值选择,我得到国家或地区名称 ML 等。此外,我不能因为他们编辑选项值在服务器端code重新使用。

I can't use ng-options here! Now, whenever I change the value from select, I get CountryName as IN, US, ML etc.. Moreover, I can't edit values of options because they're used in server side code.

在这里,我想国家或地区名称印度美国马来西亚,而不是!我该怎么办?

Here I want CountryName as India, United States or Malaysia instead! What can I do?

推荐答案

什么数据服务器送你?

是它的一些阵列像这样的对象?

Is it some array of objects like this?

var countries = [ 
    { name: 'India', code: 'IN'},
    { name: 'United States', code: 'US'},
    { name: 'Malaysia', code: 'ML'}
];

如果是,那么你可以很容易地使用NG选项(即使你没有,它只是更好)。

If yes, then you can easily use ng-options (even though you dont have to, its just better).

<select ng-model="CountryName"
    ng-options="c.code as c.name for c in countries">
</select>

或在的情况下,你实际上与服务器端的工作生成的HTML页面,没有JavaScript对象,那么它的一个小一点小技巧:
(假如你可以使用jQuery):

Or in the case that you actually are working with server side generated html page and no javascript objects, then its a little bit tricker: (Supposing you can use JQuery):

查看:

<select id="countryNameSelect" ng-change="setCountryName()">
    <option value="IN">India</option>
    <option value="US">United States</option>
    <option value="ML">Malaysia</option>
    <!-- and other 200+ records -->
</select>

控制器:

$scope.countryName = '';
$scope.setCountryName = function() {
  $scope.countryName = $("#countryNameSelect option:selected").html();
};

这篇关于角JS - 获取选定的文本为NG模式(无NG选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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