【问题标题】:Angularjs select ng-model binding for objectAngularjs为对象选择ng-model绑定
【发布时间】:2016-04-26 05:43:15
【问题描述】:

我想选择对象数组。example 但不知何故,我无法访问所选对象的属性。

js---

 $scope.test1={};
 $scope.test = [{'name':'test1'},{'name':'test2'},{'name':'test3'}];

html--

<select style="width:100px;height:25px;" ng-model="test1">
   <option  ng-repeat="attribute in test" value="{{attribute}}">{{attribute['name']}}</option>
</select>
{{test1}}
{{test1.name}}

这里,test1.name 为空白。

【问题讨论】:

  • 在这种情况下您应该使用ng-options 而不是ng-repeatoption 标签只能绑定到字符串,所以你的 test1 在这种情况下不是一个对象,它实际上是对象的字符串表示。 ng-options 旨在克服这种行为。

标签: html angularjs select angularjs-ng-model


【解决方案1】:

以这种方式使用ngOpions 进行操作。它比ng-repeat 提供适当的控制

<select style="width:100px;height:25px;" ng-model="test1"  
     ng-options="attribute.name  for  attribute in test">

这里是Plunker

【讨论】:

    【解决方案2】:

    这是因为 select value 被解释为字符串。不作为对象。当然,字符串没有 name 属性。如果您希望您的值包含整个对象,您可以使用ng-optionsRead the documentation here.

    【讨论】:

      猜你喜欢
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 2015-05-10
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多