Javascript 如果值使用angularJS匹配,则选择选项从列表中设置默认值

Javascript 如果值使用angularJS匹配,则选择选项从列表中设置默认值,javascript,html,angularjs,angularjs-ng-repeat,Javascript,Html,Angularjs,Angularjs Ng Repeat,我有html <select ng-model="user.role" class="form-control" name="role" > <option value="">Select Role</option><option ng-repeat="role in roles" value="{{role.authority}}">{{role.authority}}</option> </select>

我有html

<select  ng-model="user.role" class="form-control" name="role" >
    <option value="">Select Role</option><option ng-repeat="role in roles" value="{{role.authority}}">{{role.authority}}</option> </select>

选择角色{{Role.authority}
现在角色是一个列表,role.authority是字符串。如果role.authority==MYROLE,我想将默认值设置为选中,然后将其设置为默认值,即role.authority中的默认值


我该怎么做

您必须将相同的值分配给
选择
控制器内部的模型,在您的情况下,它会是这样的

$scope.user.role = $scope.roles[0].authority
或使用选定的

<option ng-repeat="role in roles" value="{{role.authority}}" ng-selected="role.authority == 'defaultValue'">{{role.authority}}</option>
{{role.authority}

您必须将相同的值分配给
选择
控制器内部的模型,在您的情况下,它会是这样的

$scope.user.role = $scope.roles[0].authority
或使用选定的

<option ng-repeat="role in roles" value="{{role.authority}}" ng-selected="role.authority == 'defaultValue'">{{role.authority}}</option>
{{role.authority}

its$scope.roles=data.roles。这里的角色是角色对象的列表。在HTML中,我传递selected=“someValue”,但我不传递硬编码的。我想检查role.authority是否包含“SOMEROLE”,然后将其设置为默认选中。是的,因此在此行之后,您可以分配
$scope.user.role=$scope.roles[0]。authority
其中
0
是您的默认角色。不是这样,如果(role.authority==“ROLENAME”),则我想这样做,如果(role.authority==“ROLENAME”)选中,当我调用它时,它会给我选择的数据。这次我选择角色来执行操作。现在,我可以在签入ng if或类似的内容后从列表中设置一些默认值吗?您可以将您使用此文件的链接发送给我吗?请发送,$scope.roles=data.roles。这里的角色是角色对象的列表。在HTML中,我传递selected=“someValue”,但我不传递硬编码的。我想检查role.authority是否包含“SOMEROLE”,然后将其设置为默认选中。是的,因此在此行之后,您可以分配
$scope.user.role=$scope.roles[0]。authority
其中
0
是您的默认角色。不是这样,如果(role.authority==“ROLENAME”),则我想这样做,如果(role.authority==“ROLENAME”)选中,当我调用它时,它会给我选择的数据。这次我选择角色来执行操作。现在,我可以从我的列表中设置一些默认值在签入ng或类似的东西后,你可以发送我的链接,你在哪里使用这个请。