Shouldn't ng-bind work for input as well?

孤街浪徒 提交于 2019-12-12 15:29:42

问题


I was a bit puzzled by my

<input ng-bind="x.a * x.b" tabindex="-1" readonly/>

expression not working. I can't use ng-model there (as the product is no L-value), so I blindly switched to ng-bind. I guess, it doesn't work because of the funny HTML inconsistency (using value=xxx instead of placing the value in the element text). So I switched to

<input value="{{x.a * x.b}}" tabindex="-1" readonly/>

which solved the problem, but shouldn't input ng-bind work anyway? AFAIK jQuery val() does. Am I doing something wrong?

Side questions:

  • Is it a bad practice to use inputs which are always readonly?
  • If so, what's the recommended way? span or label or what?

回答1:


ngBind set's the element's text content:

element.text(value == undefined ? '' : value);   // from the source code of Angular

So, it does not work for setting the value of an input (nor shouldn't it).

I believe it is better to use <span> in place of readonly inputs.



来源:https://stackoverflow.com/questions/23607941/shouldnt-ng-bind-work-for-input-as-well

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