最近遇见angularjs 在IE上当使用ng-options作为select的选项数据源,并且被套在ng-switch(ng-transclude)之类的,当angular上得ng-options数据源model改变后,在IE上并不渲染。

在一阵的测试和阅读相关文档后最后确认为:因为ng-switch(ng-transclude)是为了使其scope为原来的父scope,在父scope上生成了DOM后才克隆(cloneNode)到指定的指令位置。然而IE在对于select克隆的节点,不会主动去触发重绘,所以才有了上面的issue。

问题确定了,那我们所需要做的就是手动的去触发让IE对Select重绘,尝试了很多办法后最终确认有效的是:首先在options上用原生js去添加一个option,在马上移除掉这个option,所以解决方案如下:

angular.module('ie.select', [])

.directive('ieSelectFix', [

    function () {

        return {

            restrict: 'A',

            require: 'select',

            link: function (scope, element, attributes) {

                var isIE = document.attachEvent;

                if (!isIE) return;

                var control = element[0];

                scope.$watch(attributes.ieSelectFix, function () {

                    //it should be use javascript way, not jquery way.

                    var option = document.createElement("option");

                    control.add(option, null);

                    control.remove(control.options.length - 1);

                });

            }

        }

    }

]);

  

使用方式如下:

<select ie-select-fix="options" ng-model="demos" class="form-control"

        ng-options="currOption.value as currOption.text for currOption in options">

    <option value="" default>Select</option>

</select>

  

我也在我的github专门创建了一个针对angularjs在IE上issue的项目,收录了这个指令,在后续也会加入我所遇见的angularjs关于ie的issue,也希望大家帮助完善这个项目,让我们的angularjs程序在IE工作的更美好,让我们这些辛苦的程序猿也少一点加班时间,多一点陪家人,泡咖啡的时间。哈哈。

github项目地址:https://github.com/greengerong/angular-ie-enhancer

angularjs ng-option ie issue解决方案的更多相关文章

  1. angularjs 页面缓存及动态刷新解决方案

    一.准备工作 框架:angularjs ui组件库:ionic1 二.页面缓存cache 路由设置cache参数,true为缓存,false为不缓存,代码如下: angular.module('app ...

  2. Part 6 AngularJS ng repeat directive

    ng-repeat is similar to foreach loop in C#. Let us understand this with an example. Here is what we ...

  3. part 4 AngularJS ng src directive

  4. AngularJs ng-repeat重复项异常解决方案

    ng-repeat="v in arr track by $index" <!DOCTYPE html> <html lang="en"> ...

  5. 常用的Issue解决方案(EF框架)

    1.提交出错:ObjectStateManager 中已存在具有同一键的对象.    ObjectStateManager 无法跟踪具有相同键的多个对象. 遇到此问题,首先要确定的是主键是否赋值,以及 ...

  6. Part 15 AngularJS ng init directive

    The ng-init directive allows you to evaluate an expression in the current scope.  In the following e ...

  7. [AngularJS] AngularJS系列(1) 基础篇

    目录 什么是AngularJS? 为什么使用/ng特性 Hello World 内置指令 内置过滤器 模块化开发 一年前开始使用AngularJS(以后简称ng),如今ng已经出2了.虽说2已完全变样 ...

  8. 从angularJS看MVVM

    javascript厚积薄发走势异常迅猛,导致现在各种MV*框架百家争雄,MVVM从MVC演变而来,为javascript注入了全新的活力.我工作的业务不会涉及到angularJS[ng]这么重量级的 ...

  9. [Angularjs]angular ng-repeat与js特效加载先后导致的问题

    写在前面 最近在项目中遇到这样的一个前端的bug,在ng-repeat中绑定的图片,有一个晃动的特效,在手机端浏览的时候,图片有时候会正常展示,有时就展示不出来.当时猜测是因为angularjs与特效 ...

随机推荐

  1. DEV设计之自动流水号,DEV专家解答,自己折腾了半天也没有搞定,怪英文不好

    () 老外专家给了回答,结果没有全到懂,又折腾了20分钟朋友提示才搞定 获取一个自动增加1的流水号值, 第一个参数是本事的数据库连接对象,第2个参数是也这个值为唯一标识返回来一个增量的值,第三个好像没 ...

  2. maven下读取资源文件的问题(转)

    原文链接:http://shenchao.me/2016/04/20/maven%E4%B8%8B%E8%AF%BB%E5%8F%96%E8%B5%84%E6%BA%90%E6%96%87%E4%BB ...

  3. C++ exe调用dll文件

    生成dll程序 extern "C"_declspec(dllexport) void maopao(int *p,int count);void maopao(int *p,in ...

  4. C语言之数组,字符串,指针

    一. 数组的定义 1.  数组初始化 初始化方式 int a[3] = {10, 9, 6}; int a[3] = {10,9}; int a[] = {11, 7, 6}; int a[4] = ...

  5. Win10上使用SVN遇到的一些问题

    最近使用上了Win10,遇到了很多问题,前一段时间是MVC3项目的问题,今天又遇到了SVN工具使用的相关问题.我这个环境是WIN 10,64位,SVN工具是TortoiseSVN-1.9.2.2680 ...

  6. python subprocess 自动运行实验室程序

    import threading, os, subprocess, time exec_path = "/home/xhz/gems/ruby/amd...../bin/tester.exe ...

  7. loadrunner回放脚本报错27780:“[10053] 软件导致连接中止”

    录制的脚本在回放时报错,错误如下: vuser_init.c(12): 警告 -26627: 对于“http://bsp.paycenter.58.com.cn/favicon.ico”,HTTP 状 ...

  8. UDP及其组播,接收发送封装

    1.Receiver public class Receiver { public delegate void HeartBeat(byte[] data); public event HeartBe ...

  9. 天气预报API(二):全球城市、景点代码列表(“旧编码”)

    说明 2016-12-10 补充 (后来)偶然发现中国天气网已经有城市ID列表的网页...还发现城市编码有两种,暂且称中国天气网这些编码为旧标准"旧编码"的特征是 9个字符长度; ...

  10. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...