• AngularJS 的安全Apply


    $scope.safeApply = function(fn) {
        var phase = this.$root.$$phase;
        if (phase == ‘$apply‘ || phase == ‘$digest‘) {
            if (fn && (typeof(fn) === ‘function‘)) {
                fn();
            }
        } else {
            this.$apply(fn);
        }
    };

    使用NG的时候会遇到动态添加数据的情况,有可能在你动态添加数据后页面因为渲染已经完成了导致新添加的数据在页面中无法展示

    如使用select标签:

    <select id="uidSelect" class="sel" ng-model="sid" ng-init="" ng-options="user.name for user in users">
      <option value="" default>-Select one-</option>
    </select>

    因为开始的时候users数组的数据是固定的浏览器渲染出来后值就是固定的,那么如果在这之后我们想往users里面添加数据这个下拉选项是不会有改变的,这时候度娘上的各种攻略会告诉你加上 $scope.$apply() 就可以了...但是加好以后有可能会出现 [$rootScope:inprog] $apply already in progress 这样的错误信息,那么这时候只需要使用最上面提供的方法就可以了,

    先把最上面的 safeApply 方法加入你的 scope 里面,然后动态添加完数据后 使用 $scope.safeApply(); 就可以了....

  • 相关阅读:
    hdu 4027 Can you answer these queries?
    Codeforces: Empty Triangle
    hdu 3006 The Number of set
    hdu 3645 Code Management System
    进度条作控件代码
    NORMAL
    callback
    三种形状匹配脚本
    移动点动画
    脚本管理
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/5063992.html
Copyright © 2020-2023  润新知