Angularjs Select Option Selected Default

I started using angularjs in a project. I have a quest. I have 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 go through all the banks in the drop-down list. The user selects and presses SAVE. I got the identifier correctly and saved it in the database. When the user returns, I could not set the value of the drop-down list that he selected. I do this in the controller.js file

$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 letsay, which is XX Bank?

+5
source share
3 answers

You should not use ng-repeat this way. Take a look at ngOptions http://docs.angularjs.org/api/ng.directive:select

+15
source

angular . , , ng:

<option ng-repeat="bank in banks" value="{{bank.id}}" ng-selected="'11' == bank.id" >{{bank.text}}</option>

: , Lander. ng-options <select>. dropdown.assign bank.selected.id . ( ng-model - bank.id.)

<select ng-model="bank.selected.id" ng-change="someAngularfunction()" ng-options=" (''+bank.id) as (bank.id +' - '+ bank.text) for bank in banks">
                    <option value="">Select one</option>
</select>

, , = "0" "1" "3", . bank.selected.id. , , bank.id = 11 (XX Bank), bank.selected.id "11".

+8

, , . .

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

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

All Articles