在Angular $ scope中设置模型不绑定到multiselect ddl

时间:2017-04-25 15:36:31

标签: javascript angularjs

我有一个多选下拉列表绑定到一个对象数组($ scope.selectedProperties):

<select id="propertyDdl" multiple="multiple" 
ng-model="selectedProperties" 
ng-options="account.property_name group by account.client_name for account in accountInfo">
</select>

(accountInfo是包含对象数组的范围属性):

[{client_name:"Client 1", is_demo:false, property_name:"Prop 1"},
{client_name:"Client 2", is_demo:false,  property_name:"Prop 2" },
{client_name:"Client 3", is_demo:false,  property_name:"Prop 3" }]

在某个事件上,我存储了所选项目的数组:

$scope.updateSessionProperties = function () {
    CachingService.cachedSelectedProperties = $scope.selectedProperties;
};

...然后再重新申请:

if(CachingService.cachedSelectedProperties && CachingService.cachedSelectedProperties.length>0){
    $scope.selectedProperties = CachingService.cachedSelectedProperties; 
}

它所绑定的对象数组(selectedProperties)的结构与accountInfo相同:

[{client_name:"Client 1", is_demo:false, property_name:"Prop 1"}, 
{client_name:"Client 2", is_demo:false,  property_name:"Prop 2" }]

没有运气......下拉列表显示&#34;没有选择&#34;即使它绑定的scope属性是相同的对象。我也尝试过执行$ scope。$ apply(),但select元素不会重新绑定。

1 个答案:

答案 0 :(得分:0)

阅读完ng-options后,我建议如下:

<select
    id="propertyDdl"
    multiple="multiple"
    ng-model="userSelectedProperties"
    ng-options="account.property_name group by account.client_name for account in selectedProperties">
</select>