AngularJS ng-model未设置正确的值

时间:2015-01-15 10:25:48

标签: javascript angularjs angular-ngmodel

我有一张桌子,当点击一行时我会调用此函数:

 $scope.updateRow = function(row) {
     $scope.row = angular.copy(row);
}

$ scope.row 有一个名称属性,我有一个应该显示此名称的选择菜单:

<select name="rowName" ng-model="row.name" required required-message="'A name must be selected '" class="sa-select">
  <option value=""></option>
  <option ng-repeat="name in names" value="{{name.fullName}}">{{name.fullName}}</option>
</select>

但是,当点击该行时,它会将选择菜单中的选项值设置为

<option value="? string:test name ?"></option>

即使 $ scope.row.name =“测试名称”

选项值应如下所示:

<option value="test name">test name</option>

似乎是这个问题的类似问题:Angular adds strange options into select element when setting model value

1 个答案:

答案 0 :(得分:0)

<select name="rowName" ng-options="name.fullName as name.fullName for name in names" ng-model="row.name" required required-message="'A name must be selected '" class="sa-select">    

未经过测试,但您可以在此处阅读更多内容:https://docs.angularjs.org/api/ng/directive/ngOptions

如果您想添加默认选项,它将如下所示:

<select name="rowName" ng-options="name.fullName as name.fullName for name in names" ng-model="row.name" required required-message="'A name must be selected '" class="sa-select">
<option value="your value"> your text </option>
</select>