繁体   English   中英

:AngularJS:ng-selected不使用true语句

[英]:AngularJS: ng-selected is not working with a true statement

我有一个像这样的选择框:

    <select class="form-control"
            ng-model="contactName"
            ng-change="testFun(contactName)"
            ng-options="contact.Id as contact.Name + ' ' + contact.Surname for contact in company.items.contacts"
            ng-selected="$scope.contactId === contact.Id">
    </select>

其中$ scope.contactId = 14151,contact.Id = 14151

但是选择框仍然没有选择合适的人物仍然显示空白。

我无法弄清楚我在这里做错了什么。

编辑

<select class="form-control" 
        ng-model="contactName"
        ng-change="testFun(contactName)"
        ng-options="contact.Id as contact.Name + ' ' + contact.Surname + ' id:' + contact.Id for contact in company.items.contacts"
        ng-selected="contactId == contact.Id">
 </select>
 <pre>
    contactId: {{contactId}}
 </pre>

结果如下,这也证实了ID是相同的: 具有相同ID的 ContactId 选项

这个盒子仍然空白

来自AngularJS 文档

注意:ngSelected不与select和ngModel指令交互,它只在元素上设置selected属性。 如果在select上使用ngModel,则不应在选项上使用ngSelected,因为ngModel将设置选择值和选定选项。

话虽这么说,你应该使用ng-init="contactName = contactId"作为@Noman建议,它使用contactName初始化contactName contactId ,随后选择正确的选项。

使用ng-init这将起作用。 你不能在html中使用$scope 删除$scope会起作用。

<select class="form-control"
        ng-model="contactName"
        ng-change="testFun(contactName)"
        ng-options="contact.Id as contact.Name + ' ' + contact.Surname for contact in company.items.contacts"
        ng-init="contactName = contactId" >
</select>

ngSelected不适用于ngOptions,您必须使用ng-init

暂无
暂无

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

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