Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ng-bind and interpolation {{}} in Angular [duplicate]

Tags:

angularjs

Is there any difference between {{ }} and ng-bind in angular.

I am quite new to Angular. I started with using {{ }} and then in the documentation i find ng-bind. I think they do the same work but then why an extra directive, if not then please tell the difference.

like image 675
Nitin Rawat Avatar asked Sep 09 '14 09:09

Nitin Rawat


People also ask

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

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 does ng-bind do?

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 AngularJS interpolation?

In AngularJS, Interpolation is a way to transfer the data from a TypeScript code to an HTML template (view), i.e. it is a method by which we can put an expression in between some text and get the value of that expression. Interpolation basically binds the text with the expression value.


2 Answers

There is some hint in the official docs: https://docs.angularjs.org/api/ng/directive/ngBind

Typically, you don't use ngBind directly, but instead you use the double curly markup like {{ expression }} which is similar but less verbose.

It is preferable to use ngBind instead of {{ expression }} if a template is momentarily displayed by the browser in its raw state before Angular compiles it. Since ngBind is an element attribute, it makes the bindings invisible to the user while the page is loading.

like image 59
Madhur Ahuja Avatar answered Oct 07 '22 10:10

Madhur Ahuja


The most obvious difference between them is Flash of Unstyled content while using {{ ... }}.

However, there is a more subtle difference between the two if the object you pass to {{ obj }} and ng-bind="obj" is not a string.

From https://stackoverflow.com/a/19744728/987185 :

Depending on whether you use {{ ... }} or ng-bind syntax, the .toJSON and the .toString function on your object will be called to determine its representation.

like image 34
musically_ut Avatar answered Oct 07 '22 11:10

musically_ut