Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ng-binding for in AngularJS?

Tags:

angularjs

I am an AngularJS newbie and trying to figure out what class=ng-binding does in this example:

<label ng-dblclick="editTodo(todo)" class="ng-binding">fghfgh</label>

I found it here:

http://todomvc.com/architecture-examples/angularjs/#/

I use Chrome and developer tools. Is this an angular keyword? I could not find it in the manual (http://docs.angularjs.org/api/ng.directive:ngBind)

like image 536
user603007 Avatar asked Dec 30 '13 00:12

user603007


People also ask

What is the use of NG-bind in AngularJS?

The ng-bind directive tells AngularJS to replace the content of an HTML element with the value of a given variable, or expression. If the value of the given variable, or expression, changes, the content of the specified HTML element will be changed as well.

What is difference between ng-model and Ng-bind?

ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable.

What are the binding directives in AngularJS?

The ng-bind Directive in AngularJS is used to bind/replace the text content of any particular HTML element with the value that is entered in the given expression. The value of specified HTML content updates whenever the value of the expression changes in the ng-bind directive.

Does ng-bind bind the application data to HTML tags in AngularJS?

ng-bind directive binds the AngularJS Application data to HTML tags. ng-bind updates the model created by ng-model directive to be displayed in the html tag whenever user input something in the control or updates the html control's data when model data is updated by controller.


2 Answers

class="ng-binding" is used internally by Angular. For example, looking at the ngBind source we find this line that adds the class and associates the binding with it using .data:

 element.addClass('ng-binding').data('$binding', attr.ngBind);

That's why this line of Angular source (noting the double curlys on {{todo.title}} result in an ngBind):

<label ng-dblclick="editTodo(todo)">{{todo.title}}</label>

Is translated into what you see in the debugger:

<label ng-dblclick="editTodo(todo)" class="ng-binding">fghfgh</label>

So class="ng-binding" isn't something you should use. You'll find Angular frequently uses classes, comments and other markers- so you'll often see this kind of change between the original html and the Angular processed results.

like image 50
KayakDave Avatar answered Oct 08 '22 10:10

KayakDave


From the docs:

ng-binding

Usage: angular applies this class to any element that is attached to a data binding, via ng-bind or {{}} curly braces, for example. (see databinding guide)

So the class ng-binding is applied by angular dynamically, for the compiler to understand that, element has a data binding associated with it.

As a developer we don't have to worry about that unless we apply some styles to these classes.

like image 20
Asim K T Avatar answered Oct 08 '22 10:10

Asim K T