AngularJS / How to prevent IE triggering automatically inputs validation?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 23:57:34

问题


I have some page with some forms.
Each form contains some constraints on fields, like required and more.

I want to display errors on validation only when user interacts with them (UX? => yes).
Indeed, as long as the field is $pristine meaning no touched, no errors should be displayed.

I managed to achieve this requirement with a lot of browsers, except... Internet Explorer (especially IE > 10). Indeed, IE seems to consider all fields as $dirty from the beginning!

Surfing on the web, I found this "fix":

MyMainAppModule.config(['$provide', function ($provide) {
        $provide.decorator('$sniffer', ['$delegate', function ($sniffer) {
            var msie = parseInt((/msie (\d+)/.exec(angular.lowercase(navigator.userAgent)) || [])[1], 10);
            var _hasEvent = $sniffer.hasEvent;
            $sniffer.hasEvent = function (event) {
                if (event === 'input' && msie > 10) {
                    return false;
                }
                _hasEvent.call(this, event);
            };
            return $sniffer;
        }]);

Running it and... wowww IE plays nicely now. Running it then in Safari Mobile (Iphone)...disappointment. Why? Because any typed characters are taking into account AFTER event handlers like $watch etc...leading to a shift between what was expected with what occured. It's a pity since it worked well in Safari Mobile before.

So, the actual dilemma is: to give priority to Safari Mobile or IE :)

Has someone ever experimented this situation in IE? Is there some other "better" fixes that I could implement?


回答1:


RETURN _hasEvent.call(this, event);



来源:https://stackoverflow.com/questions/21440306/angularjs-how-to-prevent-ie-triggering-automatically-inputs-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!