javascript IE10+ 上的 AngularJS,带有占位符的文本区域会导致“无效参数”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20224698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:02:12  来源:igfitidea点击:

AngularJS on IE10+ ,textarea with placeholder cause "Invalid argument."

javascriptangularjsinternet-explorer-10

提问by gilamran

I'm getting "Invalid argument" when using angularJS ,TextArea with placeholder, on IE10+.

在 IE10+ 上使用带有占位符的 angularJS 和 TextArea 时,我收到“无效参数”。

This will ONLY happen when the textarea node is closed with </textarea>and will not happen when I close the textarea now on itself.

这只会在 textarea 节点关闭时</textarea>发生,当我现在关闭 textarea 时不会发生。

This will raise the "Invalid argument" exception:

这将引发“无效参数”异常:

<div ng-app>
    <input ng-model="placeholderModel" type="text"/>
    <textarea id="message" placeholder="{{placeholderModel}}" ng-model="textareaModel"></textarea>
</div>

This will work with no problems:

这将毫无问题地工作:

<div ng-app>
    <input ng-model="placeholderModel" type="text"/>
    <textarea id="message" placeholder="{{placeholderModel}}" ng-model="textareaModel"/>
</div>

Running example here: http://jsfiddle.net/huecc/

在这里运行示例:http: //jsfiddle.net/huecc/

采纳答案by Alexander

This seems to be an issue with the way you're binding to the element's placeholder - strange, I know.

这似乎是您绑定到元素占位符的方式的问题 - 我知道很奇怪。

I was able to get everything working correctly in IE using the ng-attr-placeholderdirective instead of binding directly to the attribute in the DOM.

我能够使用ng-attr-placeholder指令让 IE 中的一切正常工作,而不是直接绑定到 DOM 中的属性。

For example, instead of:

例如,而不是:

<textarea placeholder="{{placeholderModel}}" ng-model="textareaModel"></textarea>

Try this:

试试这个:

<textarea ng-attr-placeholder="placeholderModel" ng-model="textareaModel"></textarea>

Related:AngularJS v1.2.5 script error with textarea and placeholder attribute using IE11

相关:AngularJS v1.2.5 脚本错误,带有 textarea 和 placeholder 属性,使用 IE11

回答by Jon Snow

I experienced this error today and randomly stumbled upon this question. Here's what solved it for me

我今天遇到了这个错误,偶然发现了这个问题。这是为我解决的问题

Before:

前:

<textarea placeholder="{[{ 'NAME' | translate }]}" ng-model="name" name="name"></textarea>

After:

后:

<textarea placeholder="{[{ 'NAME' | translate }]}" ng-model="name" name="name"> </textarea>

Note the little space inside the textarea, that's what actually stopped IE from complaining...

请注意 textarea 内的小空间,这实际上是阻止 IE 抱怨的原因...

回答by tennisgent

I know this question is now pretty old, but thought I'd throw in my thoughts too. We ran into this issue several months ago and had to drum up a fix, so we ended up using this directive to solve the problem:

我知道这个问题现在已经很老了,但我想我也会考虑一下。几个月前我们遇到了这个问题,不得不鼓动修复,所以我们最终使用这个指令来解决这个问题:

mod.directive('placeHolder', [
    function(){
        return {
            restrict: 'A',
            link: function(scope, elem, attrs){
                scope.$watch(attrs.placeHolder, function(newVal,oldVal){
                    elem.attr('placeholder', newVal);
                });
            }
        };
    }
]);

And then you can use it in your views:

然后你可以在你的视图中使用它:

<textarea place-holder="placeholderModel" ng-model="textareaModel"></textarea>

Once your model data arrives (possibly asynchronously), the directive will add a traditional placeholderattribute to the <textarea>and it'll work as you would want.

一旦您的模型数据到达(可能是异步的),该指令将向 中添加一个传统placeholder属性<textarea>,它将按您的需要工作。

It's not the greatest solution, but it works. Hope that helps.

这不是最好的解决方案,但它有效。希望有帮助。