Is it possible to bind data of scope variable to a html that is about to bind as ng-bind-html?
ie, I have a
html ="<div>{{caption}}</div>";
and my angular template look like,
<div ng-bind-html="html"></div>
the value of scope variable caption
is set in angular controller.
So, I want to bind data in {{caption}}
.
Thanks in advance..
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.
The ng-bind-html directive is a secure way of binding content to an HTML element. When you are letting AngularJS write HTML in your application, you should check the HTML for dangerous code. By including the "angular-sanitize.
AngularJS creates a two way data-binding between the select element and the $ctrl. orderProp model. $ctrl. orderProp is then used as the input for the orderBy filter.
The bind() method attaches one or more event handlers for selected elements, and specifies a function to run when the event occurs.
You should use $interpolate not $compile.
Write controller like this:
angular.module('app', ['ngSanitize'])
.controller('MyCtrl', ['$scope', '$interpolate', function($scope, $interpolate){
$scope.caption = 'My Caption';
$scope.html = $interpolate('<div>{{caption}}</div>')($scope);
});
Then write HTML like this:
<div ng-controller="MyCtrl">
<div ng-bind-html="html"></div>
</div>
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