Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS select with manual options

We have some drop downs in our app that we can NOT use ng-options with because we need to set the title attribute on the <options> tag itself which is not possible with ng-options.

The reason we need the title is because IE < 9 chops off values that are to long and they are only visible with the title(shows up on hover of the element)

Now, to see the issue I'm having look at the following JS fiddle.

http://jsfiddle.net/nstuart/nF7eJ/ (HTML Bit)

<div ng-app="myApp" ng-controller="MyCtrl">
    <select ng-model="params.value">
        <option value="">Any</option>
        <option ng-repeat="v in options" value="{{v.value}}" title="{{v.name}}" ng-selected="{{v.value == params.value}}">{{v.name}}</option>
    </select>
    <p>{{params.value}}</p>
</div>

Ideally the select would have 'test3' selected, but you can see it gets set to an empty option instead. I understand this is from the fact that the value of ng-model is not present in the ng-options, but thats because I have not defined one!

Any ideas on how to get something like this working? Possibly even be able to add title to the options generated by ng-options so we could use that instead?

EDIT

Updated the fiddle at: http://jsfiddle.net/nstuart/nBphn/2/ to as described in the answer below.

like image 383
nick.stuart Avatar asked Feb 15 '13 17:02

nick.stuart


People also ask

How do I set default selected value in ng-options?

Use ng-init to set default value for ng-options . Save this answer.

What is Ngmodeloptions in angular?

The ng-model-options directive is used to control the binding of an HTML form element and a variable in the scope. You can specify that the binding should wait for a specific event to occur, or wait a specific number of milliseconds, and more, see the legal values listed in the parameter values below.

What is the difference between Ng-options and Ng-repeat?

ng-repeat creates a new scope for each iteration so will not perform as well as ng-options. For small lists, it will not matter, but larger lists should use ng-options. Apart from that, It provides lot of flexibility in specifying iterator and offers performance benefits over ng-repeat. Save this answer.

What is ngInit?

The ngInit directive allows you to evaluate an expression in the current scope. This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit : aliasing special properties of ngRepeat , as seen in the demo below.


3 Answers

Actually, just remove the curly braces from the expression in ng-selected and it will work:

<option ng-repeat="v in options" 
        value="{{v.value}}" 
        title="{{v.name}}" 
        ng-selected="v.value == params.value">{{v.name}}
</option>

fiddle

like image 140
satchmorun Avatar answered Nov 04 '22 15:11

satchmorun


<option ng-repeat="v in options" value="{{v.value}}" title="{{v.name}}"
        ng-selected="checkOption(v.value)">{{v.name}}</option>

$scope.checkOption = function(value){
    return($scope.params.value == value);
}

Fiddle

[UPDATE] forgot to pass value variable. Updated fiddle and answer.

like image 23
Ben Felda Avatar answered Nov 04 '22 13:11

Ben Felda


For manual options, you only need to use ng-model directive. This ng-model directive gives two way data binding for the selected option.

jsfiddle: http://jsfiddle.net/rpaul/1p9b5et8/1/

<body ng-app ng-controller="AppCtrl">
<select ng-model="selectedFruit" >
    <option value ='1'> Apple</option>
     <option value ='2'> Orange</option>
</select>
   Selected value is  {{selectedFruit}}
</body>


function AppCtrl($scope) {     

      $scope.selectedFruit = '1';
}
like image 1
Razan Paul Avatar answered Nov 04 '22 14:11

Razan Paul