简体   繁体   中英

Selected option in angular select without ng-init

I have a select using ng-options . following is my select html

 <select name="courseHighSchool" ng-model="course._highSchoolSelected" ng-init="course._highSchoolSelected = course.HighSchoolId == undefined ? undefined : {HighSchoolId: course.HighSchoolId};" ng-change="course.HighSchoolId = course._highSchoolSelected.HighSchoolId;" ng-options="item.HighSchoolName for item in TEData.RecruitHighSchools track by item.HighSchoolId" required> <option></option> </select> 

I am following tips provided in this article to set the initial selected value, which i am getting from course.HighSchoolId and my options are in collection TEData.RecruitHighSchools.

I wanted to know if there is a better way I can achieve this.

Instead of using ng-init just create a field under your $scope . Bind the field to the <select> element using the ng-model directive. That will get you the exact high school object that you are looking for.

JSFiddle

将控制器中的范围变量course._highSchoolSelected设置为默认值,这将使用该默认值呈现控件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM