【发布时间】:2014-02-26 18:25:39
【问题描述】:
首先,一些背景:
使用ui-select2 时,您必须在select2 配置对象中提供initSelection 函数。否则,您将收到错误消息(但功能不会受到影响。一切仍将按预期工作)。
为了说明,请参阅this plunkr。当您从下拉菜单中选择一个项目时,它会起作用,但您会收到以下错误:
错误:如果未定义 initSelection(),则无法调用 val()
向initSelection 添加一个空函数可修复该错误。您可以在上面的 plunkr 中取消注释以查看。
问题:
using ui-select2 in conjunction with ng-repeat 时,它只是不更新模型。
控制器:
// for this demo, `users` is injected into the controller
$scope.users = users.slice(0, 2);
$scope.select2Config = {
placeholder: 'Select User...',
query: function ( options )
{
// `users` in this demo is injected into the controller.
// in the real world this would be an ajax request
options.callback({ results: users });
},
// Without initSelection, I get the above error.
// Regardless, the model isn't updated.
initSelection: angular.noop,
formatSelection: select2format,
formatResult: select2format,
};
function select2format ( user )
{
return user.first + ' ' + user.last;
}
查看:
<ul>
<li ng-repeat="user in users">
<input type="text" ng-model="user" ui-select2="select2Config">
</li>
</ul>
从下拉列表中选择项目时,模型不会更新。如果配置中没有initSelection,我会收到上述错误,但添加它仍然不会更新模型。
Here's a plunkr demonstrating the above.
问题:
如何让ui-select2 更新ng-repeat 中的模型?
【问题讨论】:
标签: javascript angularjs angularjs-ng-repeat angular-ui jquery-select2