David Cahill
David Cahill

Reputation: 519

AngularJS ng-model not setting correct value

I have a table and when clicking on a row I call this function:

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

$scope.row has a name attribute and I have a select menu that should display this name:

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

However when clicking the row it sets the option value in the select menu to

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

even though $scope.row.name = "test name"

The option value should look like:

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

Seems to be a similar issue to this question: Angular adds strange options into select element when setting model value

Upvotes: 0

Views: 444

Answers (1)

Eduard Hallberg
Eduard Hallberg

Reputation: 1

<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">    

Not tested tho, but you can read more here: https://docs.angularjs.org/api/ng/directive/ngOptions

And if you would want to add a default option it would look like this:

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

Upvotes: 0

Related Questions