如何在AngularJS

时间:2016-06-11 11:51:04

标签: html angularjs

Html代码

    <select   class="form-control" id="txtcity" data-ng-model="Register.city" ng-options="item for item in dtcmbCity">
                                        <option value="">Please Select</option>
                                    </select>
<button type="button" ng-click="btnSet();">Set Item</button>    

Js Code

$scope.Register = {city: ''};
$scope.dtcmbCity = ['Delhi','Bombay','Chennai'];

 $scope.btnSet= function () {
        $scope.Register.city = "Delhi"        
    }

我的问题是在表单中加载显示在选择框中的数据(delhi,bombay,chennai)。然后我点击设置按钮我想将delhi设置到选择框中。

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

如果您正在寻找在与文本值相同的选项字段中设置值属性,那么这可以帮助您。

您当前的代码。

ng-options="item for item in dtcmbCity"

结果HTML。

<select class="form-control ng-pristine ng-valid ng-touched" id="txtcity"  data-ng-model="Register.city" ng-options="item for item in dtcmbCity">

<option value="" class="" selected="selected">Please Select</option>
<option label="Delhi" value="string:Delhi">Delhi</option>
<option label="Bombay" value="string:Bombay">Bombay</option>
<option label="Chennai" value="string:Chennai">Chennai</option>
</select>

如果您在ng-options中使用曲目,则html代码为

ng-options="item for item in dtcmbCity track by item"

结果HTML。

<select class="form-control ng-pristine ng-valid ng-touched" id="txtcity" data-ng-model="Register.city" ng-options="item for item in dtcmbCity track by item">
<option value="" class="" selected="selected">Please Select</option>
<option label="Delhi" value="Delhi">Delhi</option>
<option label="Bombay" value="Bombay">Bombay</option>
<option label="Chennai" value="Chennai">Chennai</option>
</select>

设置按钮单击时的值工作正常。