angularjs select標籤使用ng-repeat標籤之後,ng-model無法更新問題解決
阿新 • • 發佈:2019-01-09
angularjs select標籤使用ng-repeat標籤之後,ng-model無法更新問題,百度了一下沒有發現原因,後來採用了jquery的方式進行首次賦值,如下過程:
我也遇到過這樣的問題,由於option是使用ng-repeat生成的,ng-model首次並沒有更新,後來採用了jquery的方式,給select設定一個id,然後使用jquery的方式賦值,如下: html程式碼: <select ng-model="upName" id="up_name" > <option value="">無</option> <option ng-repeat="familyName in familyNames track by $index" value={{familyName}}">{{familyName}}</option> </select>
js程式碼: setTimeout(function () {
$("#up_name").val($scope.upName);
}, 200);
這裡有一個必須延遲一下才能賦值成功,我猜測是因為ng-repeat執行在後面,導致ng-model無法更新,有知道的前輩可以告訴我怎麼處理