:AngularJS: ng-selected 不适用于 true 语句
:AngularJS: ng-selected is not working with a true statement
我有一个这样的 select 盒子:
<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 框仍然没有 select 找到合适的人 en 仍然显示为空白。
我不知道我做错了什么。
编辑
<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>
Wich 结果如下,这也证实了 ID 是相同的:
ContactId
Option with the same ID
盒子还是空白的
使用 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 does not work with ngOptions 您必须改用 ng-init
。
来自AngularJS docs:
Note: ngSelected does not interact with the select and ngModel directives, it only sets the selected attribute on the element. If you are using ngModel on the select, you should not use ngSelected on the options, as ngModel will set the select value and selected options.
也就是说,您应该按照@Noman 的建议使用 ng-init="contactName = contactId"
,它使用 contactId
初始化 contactName
变量,随后选择正确的选项。
我有一个这样的 select 盒子:
<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 框仍然没有 select 找到合适的人 en 仍然显示为空白。
我不知道我做错了什么。
编辑
<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>
Wich 结果如下,这也证实了 ID 是相同的: ContactId Option with the same ID
盒子还是空白的
使用 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 does not work with ngOptions 您必须改用 ng-init
。
来自AngularJS docs:
Note: ngSelected does not interact with the select and ngModel directives, it only sets the selected attribute on the element. If you are using ngModel on the select, you should not use ngSelected on the options, as ngModel will set the select value and selected options.
也就是说,您应该按照@Noman 的建议使用 ng-init="contactName = contactId"
,它使用 contactId
初始化 contactName
变量,随后选择正确的选项。