选择的角度不绑定对象数组

时间:2014-10-28 05:31:53

标签: javascript angularjs

我正在使用角度选择的插件来选择任何选择元素的属性。

我的数据采用以下格式:

 $scope.pets =  [
      {
          id: '1',
          name: 'Dog',
          desc:"Something"
      },
      {
          id: '2',
          name: 'Cat',
          desc:"Something"
      },
      {
          id: '3',
          name: 'Rat',
          desc:"Something"
      }
  ];

使用ng-options显示名称的角度选择实现是:

<select multiple ng-model="myPets"  ng-options="r as r.name for r in pets" chosen>
     

我可以使用ng-options获取上述数据的下拉列表,enter image description here

但是,如果我的ng模型绑定到以下对象,如何将默认值绑定到角度选择输入框中:

  

$ scope.myPets = {                 id:'6',                 名称:'猪',                 说明:“东西”             },

1 个答案:

答案 0 :(得分:2)

您可以使用

在控制器中设置默认值
$scope.myPets= [$scope.pets[0], $scope.pets[5]];

与您的想法相比,您需要使用数组[],因为您正在使用选择倍数。您还必须直接引用现有对象或者不能识别连接的angular / javascript。

相关问题