简体   繁体   English

如何使用angular js将选择下拉列表中的值填充为默认值

[英]How to populate value in a select dropdown as default using angular js

 <select data-ng-model="userInf.role" class="span12" required>
    <option value="" selected="selected" disabled="disabled">Choose one</option>
    <option data-ng-repeat="role in roles" value="{{ role.text }}">{{ role.text }}</option>
</select>

angular

userService.getUser($scope.userInf,
        function( data ) // success
        {

        $scope.userInf = {
                username: data.userName,
                firstname: data.firstName,
                middlename: data.middleName,
                lastname: data.lastName,
                title: data.title,
                organization: data.organization,
                role: data.authorization.role,
                dateOfBirth:data.dateOfBirth,
                save: 'update'              
            };
        },

Other fileds are coming but select value is not coming 其他文件即将推出,但选择值不会到来

In inspect element i can see 在检查元素我可以看到

<select data-ng-model="userInf.role" class="span12 ng-pristine ng-valid ng-valid-required" required="">
        <option value="? string:Analyst ?"></option>
        <option value="" selected="selected" disabled="disabled">Choose one</option>
        <!-- ngRepeat: role in roles --><option data-ng-repeat="role in roles" value="Analyst" class="ng-scope ng-binding">Analyst</option>
        <option data-ng-repeat="role in roles" value="Admin" class="ng-scope ng-binding">Admin</option>
        </select>

I know that Angular has the ng-init directive for this very purpose. 我知道Angular有这个目的的ng-init指令。 Why don'y you try the ng-options directive? 为什么不尝试ng-options指令? You can use the ng-init directive side by side with ng-options, and ng-options is a little more flexible than ng-repeat. 您可以将ng-init指令与ng-options并排使用,ng-options比ng-repeat更灵活。

<select ng-model="userInf.role" ng-options="role for role in roles" ng-init="userInf.role = '' " class="span12 ng-pristine ng-valid ng-valid-required" required="">
    <option value="" disabled="disabled">Choose one</option>
</select>

The ng-init will set your ng-model, which is the value of the options defined below, to whatever you want, in this case, setting your ng-model to '' which is the value of your first option. ng-init会将您的ng-model(下面定义的选项的值)设置为您想要的任何值,在这种情况下,将ng-model设置为'',这是您的第一个选项的值。 The ng-options will populate the rest of your options for you with the values in your array. ng-options将使用数组中的值为您填充其余选项。 You might need some setup to display the correct names and values in your options. 您可能需要一些设置才能在选项中显示正确的名称和值。 Look here https://docs.angularjs.org/api/ng/directive/select 请看这里https://docs.angularjs.org/api/ng/directive/select

Yeah, I would do it like this: 是的,我会这样做:


var app = angular.module('app', [])
app.controller('exampleCtrl', function ($scope) {
  $scope.options = ["a", "b", "c"]
  $scope.selected = $scope.options[1]
})

<select ng-options="o for o in options" ng-model="selected"></select>      

Option "b" will be selected on load 选项“b”将在加载时选择

<select name="repeatSelect" id="repeatSelect" ng-model="data.repeatSelect">
      <option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name}}</option>
    </select>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM