I'm new to Angular JS
.
Can any one of you guys explain me the difference between ngBind
,ngBindHtm
& ngBindTemplate
in Angular JS
with an example?
ngBind is used to replace the text content of the specified HTML element with the value of a given expression. For example if you have an html as follows <b ng-bind="name"></b>
and in your controller give a value for name as $scope.name = "John"
. This will result in <b>John</b>
. But you can't use multiple values to bind in a single html element. For example
$scope.first_name = "John";
$scope.second_name = "D";
<b ng-bind="first_name second_name"></b>
This will not give the result as <b>John D</b>
only bind first_name. So for binding multiple values we can use ng-bind-template
$scope.first_name = "John";
$scope.second_name = "D";
<b ng-bind-template="{{first_name second_name}}"></b>
This results in <b>John D</b>
But you can't render an html tag in this both. For rendering html template we can use ng-bind-html.
$scope.name = "<b>John</b>";
<div ng-bind-html="name"></div>
This will result in John instead of showing <b>John</b>
. That means it renders the html instead of showing html tag.
Click this link to view example
ngBind :
The ngBind attribute tells Angular to replace the text content of the specified HTML element with the value of a given expression, and to update the text content when the value of that expression changes.
ngBindTemplate :
The ngBindTemplate directive specifies that the element text content should be replaced with the interpolation of the template in the ngBindTemplate attribute. Unlike ngBind, the ngBindTemplate can contain multiple {{ }} expressions. This directive is needed since some HTML elements (such as TITLE and OPTION) cannot contain SPAN elements. ngBindTemplate only runs "strings"
A simple metaphor for the difference:
ngBind only runs "objects".
ngBindTemplate only runs "strings"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With