jQuery ng-model 绑定不适用于 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17784266/
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-08-26 19:57:40  来源:igfitidea点击:

ng-model binding not working on div

javascriptjqueryhtmlangularjs

提问by Hyman

The two binding of AngularJS is not working on div. I want my div to be editable but it's not happening. Could anybody please suggest what could be wrong? The same binding works fine on textbox. This is my code:

AngularJS 的两个绑定在 div 上不起作用。我希望我的 div 是可编辑的,但它没有发生。任何人都可以请建议可能有什么问题吗?相同的绑定在文本框上工作正常。这是我的代码:

<input type="text" ng-model="userProfile.firstName"/>
<div class="profile-name" contenteditable="true" ng-model="userProfile.firstName"></div> 

采纳答案by Sergiy Kozachenko

Please try this, it might help https://github.com/angular/angular.js/issues/528

请试试这个,它可能会有所帮助 https://github.com/angular/angular.js/issues/528

This is a 3 year old open issue that tons of people would like fixed. It's marked as a post-1.2-milestoneand a featurerather than a bug. Seeing as Angular.js is on release 1.2.13 as of Feb 2014, it looks like no one on the team cares about this issue at the moment. As a result you'll be better off rolling your own code or sticking with a input/textareacontrol.

这是一个 3 年前的公开问题,很多人都想修复。它被标记为一个post-1.2-milestone和一个feature而不是一个错误。鉴于 Angular.js 于 2014 年 2 月发布了 1.2.13 版,目前看来团队中没有人关心这个问题。因此,最好滚动自己的代码或坚持使用input/textarea控件。

回答by Joe Minichino

that's the correct behaviour. from angular docs "ng-model Is a directive that tells Angular to do two-way data binding. It works together with input, select, textarea. You can easily write your own directives to use ngModel as well."

这是正确的行为。来自 angular 文档“ng-model 是一个指令,它告诉 Angular 进行双向数据绑定。它与 input、select、textarea 一起工作。您也可以轻松编写自己的指令来使用 ngModel。”

in other words, if you want it to work with a div declare a directive which binds the model to the innerhtml of the div. docs here

换句话说,如果您希望它与 div 一起使用,请声明一个将模型绑定到 div 的 innerhtml 的指令。文档在这里

回答by whoami

use ng-bind.

使用ng-bind

<ANY
  ng-bind="expression">
...
</ANY>

Check https://docs.angularjs.org/api/ng/directive/ngBind.

检查https://docs.angularjs.org/api/ng/directive/ngBind