Angular throws "Error: Invalid argument." in IE

Posted by przno on Stack Overflow See other posts from Stack Overflow or by przno
Published on 2014-05-27T14:24:53Z Indexed on 2014/05/31 21:28 UTC
Read the original article Hit count: 150

I have a directive which takes element's text and places wbr elements after every 10th character. I'm using it for example on table cells with long text (e.g. URLs), so it does not span over the table. 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.

Here is jsFiddle, when clicking on the button you can see the error in console.

So obvious question: why is the error there, what is the source of it, and why only in 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.)

P.S.: I know IE does not know the wbr at all, but that is not the issue.

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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about angularjs