AngularJS data bind in ng-bind-html?

不想你离开。 提交于 2019-11-27 01:44:14
Jennie Ji

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>

You need to compile your HTML snippet, but it is recommended to do that inside the directive.

app.controller('MyCtrl', function($compile){
  $scope.caption = 'My Caption';
  $scope.html = $compile('<div>{{caption}}</div>')($scope);
});
<div ng-bind-html="html"></div>

What about?

html = '<div ng-bind="caption"></div>';

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!