Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-model binding not working on div

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:

<input type="text" ng-model="userProfile.firstName"/>
<div class="profile-name" contenteditable="true" ng-model="userProfile.firstName"></div> 
like image 832
Jack Avatar asked Jul 22 '13 09:07

Jack


People also ask

Does ngModel work on Div?

Angular ngModel can't support div(contentEditable=true) · Issue #9796 · angular/angular · GitHub.

How ngModel works in Angularjs?

ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form.

How do you bind ngModel in angular 12?

Use the ngModel selector to activate it. It accepts the domain model as an optional Input if you have a one-way binding to ngModel with the [] syntax; changing a value of the domain model in the component class sets a value in the view.

How does ngModel work?

NgModel works using these two bindings together. First, it passes the data from the component class and set data in FormControl. Second, it passes the data from UI after a specific action is triggered and then changes the value of the control.


3 Answers

Please try this, it might help 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-milestone and a feature rather 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/textarea control.

like image 80
Sergiy Kozachenko Avatar answered Oct 18 '22 01:10

Sergiy Kozachenko


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."

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

like image 32
Joe Minichino Avatar answered Oct 18 '22 02:10

Joe Minichino


use ng-bind.

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

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

like image 35
whoami Avatar answered Oct 18 '22 01:10

whoami