0

hi I am developing web application in angularjs. I have one dropdownlistbox with some values. Below is my dropdownlistbox html code.

<select id="calc-year" ng-model="period" name="period" required range-numberddwn="rangeddwn">
   <option value="" label="{{ 'Month' | translate }}">{{ 'Month' | translate }}</option>
   <option ng-repeat="x in cal" value="{{x.Period}}">{{x.Period}} {{'Months' | translate}}</option>
</select>

Below is my code to assign values to dropdownlistbox.

$scope.cal = response.data.data.Period;

Below is the screenshot of data i am assigning to cal.

Dropdown values

If i want to set 4th value by default then i tried as below.

$scope.period = $scope.cal[4]; and $scope.period = response.data.data.Period[4];

Whenever i set as above i blank value will come in dropdownlist

Above both options did not work out for me. May i know what is the solution to set default value? Any help would be appreciated. Thank you.

6
  • use ng-init for set default value Commented Sep 27, 2017 at 4:30
  • Thank you i want to assign it in controller. Commented Sep 27, 2017 at 4:34
  • then you have to use ng-options ,look this :docs.angularjs.org/api/ng/directive/ngOptions Commented Sep 27, 2017 at 4:36
  • Possible duplicate of how to set dropdown default value using angularjs Commented Sep 27, 2017 at 4:38
  • The problem can be caused by fact, that you mixed option ng-repeat and separate option value=, try to merge them into the one option ng-repeat Commented Sep 27, 2017 at 5:40

2 Answers 2

1

In your controller, set a value in scope variable which you want to be the default value for your drop down. Make sure, cal variable exist in scope. Then try something like this,

$scope.calValue = $scope.cal[3]; //This will be Default cal value

Then in your view(html), use ng-options something like this,

<select ng-model="calValue" ng-options="x.Period for x in cal"></select>

Now, calValue will be the default value in drop down, and if you select any other option from dropdown, then that will become the value of calValue.

Cheers !!

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

3 Comments

You saved my day. Thanks
also i want to know instead of setting using index like $scope.cal[3] how can i set using value?
Iterate over the array/list (using loop), compare your required value in array element`s values (using if-else), and as per the outcome of comparison assign that array element to default value. You can write this code in controller itself.
1

If you want to set the 4th value index should be 3

 $scope.period = $scope.cal[3]; 
 $scope.period = response.data.data.Period[3];

1 Comment

Thanks but i just gave example but problem is i am not able to set any of the values. Whenever i set $scope.period = $scope.cal[3]; i get blank in dropdownlistbox.

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.