Angular JS - 获取选定的文本作为ng模型(没有ng选项)
Angular JS - get selected text as ng model (without ng-options)
在我的代码隐藏中,我按如下方式填写了一个 DropDownList(使用 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")
然后客户端,我有一个选择标签,里面充满了服务器端的选项,如下所示:
<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-options
!现在,每当我从 select
更改值时,我都会CountryName
为 IN
、US
、ML
等。此外,我无法编辑选项的值,因为它们用于服务器端代码。
在这里,我希望CountryName
India
,United States
或Malaysia
!我能做什么?
服务器向您发送了哪些数据?
是这样的对象数组吗?
var countries = [
{ name: 'India', code: 'IN'},
{ name: 'United States', code: 'US'},
{ name: 'Malaysia', code: 'ML'}
];
如果是,那么您可以轻松地使用 ng-options(即使您不必这样做,它更好)。
<select ng-model="CountryName"
ng-options="c.code as c.name for c in countries">
</select>
或者,如果您实际上正在使用服务器端生成的html页面并且没有javascript对象,那么它就有点棘手了:(假设您可以使用 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();
};
在 PHP 后端数组中 Json Out: <? PHP echo json_encode($arrayDemo); ?>
// Init Load
$(function () {
'use strict';
angular
.module('ngjsapp', [])
.controller('FormCtrl', ['$scope', function ($scope) {
var myArraySelect = <?php echo json_encode($arrayDemo); ?>
$scope.changeSenasaProvinciasId = function (id) {
console.log(myArraySelect[id]);
}
}]);
});
只有 HTML Stact + jQuery
// Option 1
$(function () {
'use strict';
angular
.module('ngjsapp', [])
.controller('FormCtrl', ['$scope', function ($scope) {
$scope.changeSenasaProvinciasId = function () {
console.log($("#mySelectID option:selected").text());
}
}]);
});
只有HTML Stact + jQuery BEST
。// Option 2
$(function () {
'use strict';
angular
.module('ngjsapp', [])
.controller('FormCtrl', ['$scope', function ($scope) {
var myArraySelect = $('#mySelectID option').each( function() {
myArraySelect.push({$(this).val() : $(this).text() });
});
$scope.changeSenasaProvinciasId = function (id) {
console.log(myArraySelect[id]);
}
}]);
});
相关文章:
- 角度无线电按钮ng模型不起作用
- ng模型内的ng重复的ng重复开始
- 将输入值设置为ng模型属性[Angular]时出现问题
- 自定义指令中的AngularJS ng模型
- 如何为动态创建的文本区域中输入的值更新ng模型
- ng重复中的ng模型-初始化唯一作用域属性
- ng重复变量到ng模型
- 初始化ng模型时,Angular ui选择占位符不起作用
- 角度去抖动(ng模型选项)不起作用
- 即使 ng 模型有值,也使输入字段为空
- 选择框中带有关联的ng模型,选项中带有ng重复
- 为什么我的指令(使用链接)没有出现,并允许ng模型看到它
- AngularJS+IE 11+聚合物=ng模型未更新
- ng模型在$(element).clone()之后不起作用
- 为什么ng选项指令需要ng模型
- 使用引导时间选取器时,没有更新数据ng模型值
- 具有ng重复的动态ng模型
- 有没有一种方法可以在设计模式下将ng模型或工厂绑定到iframe
- 删除ng模型中的ng模型输入值
- Angular.js默认选中不'不适用于ng模型