Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs Select Option Selected Default

Tags:

angularjs

I started using angularjs in a project. Here i have a quest. I have the below HTML

<div>
<label for="lastname">Bank Name :</label> 
<select ui-select2 ng-model="bank.id">
    <option></option>
    <option ng-repeat="bank in banks" value="{{bank.id}}">{{bank.text}}</option>
</select>
</div>

I iterate all the banks to the dropdown. User selects and press SAVE. I correctly got the id and save it to the DB. When the user comes back i could not set the value of the drop down to the one he selected. I do this in the controller.js

$http.get('/AdServerLongTail/api/user').
success(function(data, status, headers, config) {
    if(status == 200){
        $scope.id = (data["id"]);// user id                 
        $scope.bank.id = (data["bankId"]);                  
    }
}).
error(function(data, status, headers, config) {
    alert("fail");
}); 

How can I set it to bankID 11 letssay, which is XX Bank?

like image 352
Yagiz Ozturk Avatar asked Apr 30 '13 11:04

Yagiz Ozturk


1 Answers

You should NOT use ng-repeat in this way. Please look at ngOptions http://docs.angularjs.org/api/ng.directive:select

like image 137
astone26 Avatar answered Nov 02 '22 17:11

astone26