AngularJS选择下拉列表 - 选择默认选项

时间:2014-10-21 15:17:35

标签: javascript angularjs

我有AngluarJs下拉列表,每个选项“onchange”将导航到另一个页面。这是代码:

   <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);
     };       

此处的问题是当我点击$scope.suggested[0];的默认选项时,它不会导航用户。有人可以建议我在做错误吗?

3 个答案:

答案 0 :(得分:2)

这是正常的,因为当你选择$ scope.suggested [0]时没有变化它是你的默认值。

防止它的一种方法是添加空白选项

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

这样,当你选择$ scope.suggested [0]时,会有一个变化。

答案 1 :(得分:0)

只需在你的javascript中触发changeOption函数:)

您正在选择默认选项,但您需要在值更改时触发该功能..所以只需将代码修改为:

     $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

答案 2 :(得分: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 );
};

使用角度js,在div标签中添加ng-model。