Angular injects “string:” before value in ng-options

人走茶凉 提交于 2019-11-30 23:23:59

This behavior is documented in the angular changelogs, as a breaking change for Angular 1.4-beta.0. https://github.com/angular/angular.js/blob/master/CHANGELOG.md#breaking-changes-17

Basically, in order to preserve duplicate checking within ng-options, if a list of primitive values are supplied to ng-options, the value's type is prepended to the value in order to create a unique hash key to track (this differs from earlier versions of angular, which would track by the index or the key of the item in the collection).

In practice, this should not affect most usages of ng-options. If it is important for you to preserve the value parameter of the dropdown, you can use the track by parameter in ng-options in order to provide an alternate key to be used for duplicate tracking.

Note that this differs from ng-repeat, in that ng-repeat cannot generate a surrogate key using this method, since it would affect the behavior of the repeated elements. Therefore if a duplicated list is passed to ng-repeat without a track by clause, ng-repeat will produce an error and not render the items, where ng-options is able to render the list.

Here is the working Plunker

And JS:

    <select ng-model="template"    ng-options="c as (c.label + ' - ' + c.name) for c in options">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!