6

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?

3 Answers 3

16

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

Sign up to request clarification or add additional context in comments.

4 Comments

I hear you. But, just because an external library does not support it, does not mean it is correct.
@Lander, I agree it's not correct, but unfortunately it's the only way to use select2. You can find something different or keep it this way. I would suggest writing a better directive to access s2, since the idea behind directives is providing an API without exposing the way it works internally. In ideal world the directive API should work with ng-select. Using ng-repeat won't break anything, and I wouldn't consider it a hack. What bothers me more is the select2 can be is horribly slow in angular.
Thanks for the reply. But, as you stated, there is an issue of performance. Using ng-repeat in this way has a record of bad performance.
Using ng-repeat for generating options leads to performance problems with as few as hundred options.
11

I am new to angular and struggled for this a bit. Here is the simple tag that does that , and it is ng-selected:

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

But again: Dont do it this way as advised by Lander. Rather use ng-options in <select>. Otherwise you will end up with empty first option in the dropdown.assign bank.selected.id in model.(here in your case ng-model is 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>

Also remember that the option value will not show the bank-id in the options,rather it will show option value="0" or "1" or "3" which is index. But selected value in dropdown will be bound with bank.selected.id model. For example, if dropdown value is selected as for say bank.id=11 (XX Bank), the bank.selected.id will be set as "11".

Comments

2

I found it, so easy how I did missed that. Here is the correct code.

<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 Comments

Is there a way to do this without Angular UI? I'm working on it, too.
Using repeat nested in options causes problems and doesn't work right with regular angular binding. It took me a bit to figure out why this technique was causing other Angular db to fail...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.