我正在尝试以编程方式更新select2模型并让视图刷新但它似乎无法正常工作。
这是一个从Angular UI项目分叉的示例plunker:http://plnkr.co/edit/kQROgr?p=preview
我尝试添加initSelection()为select2 docs(http://ivaynberg.github.com/select2/“对外部值更改做出反应”),但这不起作用。我也尝试过使用select2 3.3.2并且没有解决它。
有两个问题: 1)单击“更新模型”,模型会更新,但不会向select2小部件添加标签。也 2)单击“Update-Model”,然后使用select2选择第二个标签,“Update-Model”添加的第一个标签消失。
答案 0 :(得分:0)
我知道这个问题有点老但是嘿......我找到了它并且不知道答案。
通过设置模型然后在select2Options配置上调用initSelection(),我设法做了我想要的事情
所以我的配置是这样的:
$scope.select2Options = {
allowClear: true
minimumInputLength: 3
quietMillis: 1000
initSelection: ->
$scope.property
query: (query)->
Properties.query({q: query.term}, (response)->
data = {results: processData(response['properties'])}
query.callback(data)
)
processData = (data)->
results = []
angular.forEach(data, (item, index)->
results.push(item)
)
return results
}
然后让我的模态返回新创建的对象,如下所示:
modalInstance.result.then((result)->
$scope.property = result
$scope.select2Options.initSelection()
)
基本上,一旦更新了模型,我就必须手动重新初始化select2小部件。我认为这可以用$ scope来处理。$ watch如果你真的想要,但这可能是一种浪费,除非你从一些地方或某些地方更新了房产。