angularjs - Dynamic Select List Default Selected Item - Angular JS -


i have code listed below works fine, when attempt add ng-model , related ng-change select, empty option added. understanding why, because on init selectedoption ng-model empty.

what want set default value when can use ng-change set options isselected value true when user selects it. i'm not sure how go this, have no issues doing when i'm working static generated select list, reason can't figure out dynamic generated list.

any input appreciated!

    <div ng-repeat="optiontype in productdetailmodel.optiontypes">          <select name="{{optiontype.optiontypename}}">             <option ng-repeat="option in optiontype.options"              value="{{option.optionvalue}}">{{option.optionvalue}}             </option>          </select>      </div> 

here's plunkr mocked give basic idea of have in mind: http://plnkr.co/edit/xbdfc0xzdwsf0mbifzov?p=preview

the initial option blank because model undefined. tymejv said, initializing scope inside of .js define default value shown selected initially.

$scope.modelname = $scope.optiontype.options[0]; 

it might helpful use ng-options instead of ng-repeat. 1 reason why might beneficial ng-options not create child scope (unlike ng-repeat). simplify linking model , avoid confusion.

<select name="{{optiontype.optiontypename}}" ng-model="modelname" ng-options="option option in optiontype.options">  </select> 

this article covers basics of ng-options discusses why used opposed ng-repeat.

hope helps.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -