简体   繁体   English

选择中的AngularJS默认选项在IE9中不起作用

[英]AngularJS default option in select not working in IE9

I have a form in my AngularJS application, which contains dropdowns with default options. 我的AngularJS应用程序中有一个表单,其中包含带有默认选项的下拉列表。 I use ng-init and ng-selected to make this happen. 我使用ng-init和ng-selected来做到这一点。 It works fine in Chrome, but in Internet Explorer 9, when i first load the page, what shows up on the dropdown is the literal angularjs expression, ie "{{ showMethod(args) }}". 它在Chrome中工作正常,但是在Internet Explorer 9中,当我第一次加载页面时,下拉菜单中显示的是原语angularjs表达式,即“ {{showMethod(args)}}”。 When i go and change the option, the options are there as they should. 当我去更改选项时,这些选项就在那里。 It is just initially that the expression doesn't get expressed. 只是一开始,表达式没有被表达。

Here is my HTML code: 这是我的HTML代码:

<td>
    <div ng-if="var.methods.length==0">NA</div>
    <div ng-if="var.methods.length>0">
        <select ng-init="selectedmethods[var.id]=defaultMethod(var.id,var.methods)" ng-model="selectedmethods[var.id]" >
           <option ng-repeat="method in var.methods" value="{{method.id}}" ng-selected="isMethodSelected(var.id,method)">{{ showMethod(method,var.id)}}</option>
        </select>
    </div>

This is part of another ng-repeat. 这是另一个ng-repeat的一部分。
Initially, i set the selected method to the default method for that variable using ng-init. 最初,我使用ng-init将所选方法设置为该变量的默认方法。 Using ng-selected and a method called isMethodSelected, i check if the method has been selected by the user by checking the selectedmethods object and if not, check if the method is a defaut method. 使用ng-selected和一个称为isMethodSelected的方法,我通过检查selectedmethods对象来检查用户是否选择了该方法,如果没有,请检查该方法是否为defaut方法。 Then, i display the method name using showMethod function, which i added to see if this would help the issue in IE (which it didn't). 然后,我使用showMethod函数显示该方法的名称,并添加了该函数以查看它是否对IE中的问题有所帮助(不是)。 What i had before was just the expression method.formattedMethodName. 我以前只有表达式method.formattedMethodName。

Here is part of my JS code: 这是我的JS代码的一部分:

$scope.isMethodSelected = function(varid,method) {
       var sel = false;
       if($scope.request.specsByVar.hasOwnProperty(varid) && $scope.request.specsByVar[varid].method.id==method.id) {
           sel = true;
       } else if(!$scope.request.specsByVar.hasOwnProperty(varid) && method.defaultMethod) {
           sel = true;
       }
       return sel;
   }

   $scope.defaultMethod = function(varid,methods) {
       var defaultMethod;
       if($scope.request.specsByVar.hasOwnProperty(varid)) {
           defaultMethod = $scope.request.specsByVar[varid].method.id;
       } else {
           for (var i = 0; i < methods.length; i++) {
               if(methods[i].defaultMethod) {
                   defaultMethod = methods[i].id;
                   break;
               }
            }
       }
       return defaultMethod;
   }

   $scope.showMethod = function(method,varid) {
       if(method) {
           return method.formattedMethodName;
       } else {
           return $scope.selectedmethods[varid].formattedMethodName;
       }
    };

This is apparently an IE issue. 这显然是IE问题。 Does anyone have a solution? 有没有人有办法解决吗?

Thanks, Olga 谢谢,奥尔加

I was able to solve this issue by using ng-options: 我可以使用ng-options解决此问题:

    <select ng-init="selectedmethods[var.id]=defaultMethod(var.id,var.methods)" 
         ng-model="selectedmethods[var.id]" title="{{ selectedmethods[var.id].formula }}" 
         ng-options="method.id as method.formattedMethodName for method in var.methods | filter:{active:true}">

    </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM