简体   繁体   English

IE中的Angular throws“Error:Invalid argument。”

[英]Angular throws “Error: Invalid argument.” in IE

I have a directive which takes element's text and places wbr elements after every 10th character. 我有一个指令,它接受元素的文本,并在每第10个字符后放置wbr元素。 I'm using it for example on table cells with long text (eg URLs), so it does not span over the table. 我在例如具有长文本(例如URL)的表格单元格上使用它,因此它不会跨越表格。 Code of the directive: 指令代码:

myApp.directive('myWbr', function ($interpolate) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            // get the interpolated text of HTML element
            var expression = $interpolate(element.text());

            // get new text, which has <wbr> element on every 10th position
            var addWbr = function (inputText) {
                var newText = '';
                for (var i = 0; i < inputText.length; i++) {
                    if ((i !== 0) && (i % 10 === 0)) newText += '<wbr>'; // no end tag
                    newText += inputText[i];
                }
                return newText;
            };

            scope.$watch(function (scope) {
                // replace element's content with the new one, which contains <wbr>s
                element.html(addWbr(expression(scope)));
            });
        }
    };
});

Works fine except in IE (I have tried IE8 and IE9), where it throws an error to the console: Error: Invalid argument. 工作正常,除了IE(我已经尝试过IE8和IE9),它向控制台抛出一个错误: Error: Invalid argument.

Here is jsFiddle , when clicking on the button you can see the error in console. 这是jsFiddle ,当点击按钮时,你可以在控制台中看到错误。

So obvious question: why is the error there, what is the source of it, and why only in IE? 如此明显的问题:为什么错误存在,它的来源是什么,为什么只有在IE?

(Bonus question: how can I make IE dev tools to tell me more about error, like the line from source code, because it took me some time to locate it, Error: Invalid argument. does not tell much about the origin.) (额外的问题:如何让IE开发工具告诉我更多关于错误的信息,比如来自源代码的行,因为我花了一些时间找到它, Error: Invalid argument.并没有说明原点。)

PS: I know IE does not know the wbr at all, but that is not the issue. PS:我知道IE根本不知道wbr,但这不是问题。

Edit: in my real application I have re-written the directive to not to look on element's text and modify that, but rather pass the input text via attribute, and works fine now in all browsers. 编辑:在我的实际应用程序中,我重写了指令,不查看元素的文本并修改它,而是通过属性传递输入文本,现在在所有浏览器中都能正常工作。 But I'm still curious why the original solution was giving that error in IE, thus starting the bounty. 但我仍然很好奇为什么原始解决方案在IE中给出了这个错误,从而开始了赏金。

Your attempt to create a directive to inject elements into your string runs into a known bug in IE dealing appendChild method. 您尝试创建将元素注入字符串的指令会遇到IE中处理appendChild方法的已知错误。 I was able to reproduce your error IE with your fiddle even after removing the <div> appending completely. 即使在完全删除<div>之后,我也可以用你的小提琴重现你的错误IE。

Notice, that if you remove your div concatenation completely, and attempt to return newText in original form, you still get 请注意,如果您完全删除了div连接,并尝试以原始形式返回newText ,那么您仍然可以获得

Error: Invalid argument. 错误:参数无效。

Transclusion on UI view not working on IE9-10-11 在UI视图上的转换不适用于IE9-10-11

My "error invalid argument anonymous function call" was being thrown from me doing the following on the code below. 我的"error invalid argument anonymous function call"正在抛出我在下面的代码上执行以下操作。

Error: 错误:

<span data-ng-bind="name.first">{{name.first}}</span>

No Error: 没错误:

<span data-ng-bind="name.first"></span>

Not an Angular answer (and an old question, I know), but I'd suggest you might be taking the wrong approach to the problem. 这不是一个Angular的答案(我知道这是一个老问题),但我建议你可能采取错误的方法解决问题。 Rather insert breaks (which would ruin any markup in the string for example), simply use the CSS text-overflow or overflow property. 而是插入中断(例如,这会破坏字符串中的任何标记),只需使用CSS文本溢出或溢出属性。

I got same error in IE when I had following error in HTML template: 当我在HTML模板中出现以下错误时,我在IE中遇到了同样的错误:

<textarea disabled  [(ngModel)]="some.var">{{some.var}}</textarea>

disabled textarea does not allow ngModel in IE 禁用 textarea不允许在IE中使用ngModel

Chrome, FF did not complain, just IE Edge. Chrome,FF没有抱怨,只是IE Edge。 It is a good idea, to check the template too 检查模板也是个好主意

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

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