【问题标题】:Setting model in Angular $scope is not binding to multiselect ddl在 Angular $scope 中设置模型未绑定到多选 ddl
【发布时间】:2017-04-26 02:04:17
【问题描述】:

我有一个绑定到对象数组的多选下拉列表 ($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" }]

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

【问题讨论】:

  • accountInfo 是在哪里定义的?
  • $scope.accountInfo 定义在 $scope 开头,是一个与模型相同的数组:[{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"... }] 有趣的是,如果我推送一个从 accountInfo 到 selectedProperties 中的项目,绑定确实有效,但是以任何其他方式将相同的项目推入 selectedProperties 失败。

标签: javascript angularjs


【解决方案1】:

看完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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 2016-11-13
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    相关资源
    最近更新 更多