Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS data bind in ng-bind-html?

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

like image 683
Saidh Avatar asked Dec 27 '13 06:12

Saidh


People also ask

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.

What is Ng-bind-HTML in AngularJS?

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.

How can you bind an element to data in AngularJS?

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.

What is bind in HTML?

The bind() method attaches one or more event handlers for selected elements, and specifies a function to run when the event occurs.


1 Answers

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>
like image 72
Jennie Ji Avatar answered Oct 13 '22 23:10

Jennie Ji