1

I have AngluarJs dropdown, each option "onchange" will take navigate to another page. Here is the code:

   <select class="qtDropDown" 
           ng-model="Suggested" 
           ng-change="changeOption()" 
           ng-options="option.name for option in suggested">

Js.

     $scope.suggested = StrategyService.getPredefinedStrategies();
     $scope.Suggested = $scope.suggested[0];

     $scope.changeOption = function () {
        var selectedStrategy = $scope.Suggested;
        var queryString = selectedStrategy.name.split(' ').join(''); 
        window.location.href(baseUrl+queryString);
     };       

The Problem here is When I click on the default selection which is $scope.suggested[0]; it is not navigating the user. Can anybody suggest where I am doing the mistake please ?

2
  • So you mean, change event doesn't fire unless you change actually options? Commented Oct 21, 2014 at 15:34
  • Not exactly, the change event is firing if you select any other option not default one which is $scope.suggested[0]. Commented Oct 21, 2014 at 15:39

3 Answers 3

2

It's normal as when you select $scope.suggested[0] there is not change has it is your default.

One way to prevent it is by adding a blank option

<select class="qtDropDown" ng-model="Suggested" ng-change="changeOption()" 
ng-options="option.name for option in suggested">
    <option value=""> - Choose one of the following - </option>
</select>

That way, when you will select $scope.suggested[0], there will a change trigerred.

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

1 Comment

Thank you for the suggestion. That's true but, I wanted to have a default option. That's the point. Any way, thank you for the suggestion.
0

just trigger the changeOption function in your javascript :)

you are selecting the default option but you have the function that needs to be triggered when the value changes.. so just modify the code to :

     $scope.suggested = StrategyService.getPredefinedStrategies();

     $scope.changeOption = function () {
        var selectedStrategy = $scope.Suggested;
        var queryString = selectedStrategy.name.split(' ').join(''); 
        window.location.href(baseUrl+queryString);
     };     
     $scope.Suggested = $scope.suggested[0];
     $scope.changeOption(); // this will trigger the change of the default value

Comments

0
<select name="qtDropDown" id="qtDropDown">
<option value="url">...</option>

var urlmenu = document.getElementById( 'Select' );
urlmenu.onchange = function() {
window.open(  this.options[ this.selectedIndex ].value );
};

with angular js, add ng-model in your div tag.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.