AngularJS - 需要帮助在绑定文本输入中显示字符串的当前长度

时间:2022-08-26 12:02:41

I just started with Angular a couple days ago so simple stuff still occasionally gets the best of me. I'm trying to show the current character count of a textarea. I thought surely this should work:

我刚刚开始使用Angular几天前这么简单的东西偶尔会让我感觉最好。我正在尝试显示textarea的当前字符数。我当然认为这应该有效:

<textarea ng-model="mystring"></textarea>
<br>
<span> {{mystring.length()}} </span>

It doesn't work at all.. But why? And how do I accomplish this?

它根本不起作用..但为什么呢?我该如何做到这一点?

If I remove ".length()" the content of the textarea is showing up like it should so the binding is obviously working...

如果我删除“。length()”,textarea的内容就会显示出来,因此绑定显然正在工作......

EDIT: So it works in chrome etc but not in IE, or more specifically, the embedded browser control in a .Net winform. hooray.

编辑:所以它适用于Chrome等,但不适用于IE,或更具体地说,是.Net winform中的嵌入式浏览器控件。万岁。

2 个解决方案

#1


20  

The length of a string is a property in javascript. So this should work

字符串的长度是javascript中的属性。所以这应该工作

<span> {{mystring.length}} </span>

#2


1  

The .Net browser control acts like IE7 by default so lots of things don't really work. In an attempt to remedy this I added a registry entry as indicated in this MSDN article to force the browser to act like IE9 but still had the same problem with string.length in the expression as well as a new "object doesn't support this action" error being thrown.

.Net浏览器控件默认情况下就像IE7一样,所以很多东西都不起作用。为了解决这个问题,我添加了一个注册表项,如本MSDN文章中所示,强制浏览器像IE9一样工作,但仍然在表达式中遇到与string.length相同的问题以及新的“对象不支持这个行动“抛出错误。

At this point I gave up and decided to use this WebKitdotNet control. It adds 30 or so MB to my install size but it actually works without having to change a lot of javascript or muck about in the registry.

此时我放弃了并决定使用这个WebKitdotNet控件。它增加了30个左右的MB到我的安装大小,但它实际上工作,而无需在注册表中更改大量的javascript或muck。

#1


20  

The length of a string is a property in javascript. So this should work

字符串的长度是javascript中的属性。所以这应该工作

<span> {{mystring.length}} </span>

#2


1  

The .Net browser control acts like IE7 by default so lots of things don't really work. In an attempt to remedy this I added a registry entry as indicated in this MSDN article to force the browser to act like IE9 but still had the same problem with string.length in the expression as well as a new "object doesn't support this action" error being thrown.

.Net浏览器控件默认情况下就像IE7一样,所以很多东西都不起作用。为了解决这个问题,我添加了一个注册表项,如本MSDN文章中所示,强制浏览器像IE9一样工作,但仍然在表达式中遇到与string.length相同的问题以及新的“对象不支持这个行动“抛出错误。

At this point I gave up and decided to use this WebKitdotNet control. It adds 30 or so MB to my install size but it actually works without having to change a lot of javascript or muck about in the registry.

此时我放弃了并决定使用这个WebKitdotNet控件。它增加了30个左右的MB到我的安装大小,但它实际上工作,而无需在注册表中更改大量的javascript或muck。