AngularJS 数据绑定在 ng-bind-html 中? [英] AngularJS data bind in ng-bind-html?

查看:29
本文介绍了AngularJS 数据绑定在 ng-bind-html 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将范围变量的数据绑定到即将绑定为 ng-bind-html 的 html?

即,我有一个

html ="

{{caption}}

";

我的角度模板看起来像,

<div ng-bind-html="html"></div>

范围变量caption的值在角度控制器中设置.

所以,我想在{{caption}}中绑定数据.

提前致谢..

解决方案

你应该使用 $interpolate 而不是 $compile.
像这样编写控制器:

angular.module('app', ['ngSanitize']).controller('MyCtrl', ['$scope', '$interpolate', function($scope, $interpolate){$scope.caption = '我的标题';$scope.html = $interpolate('<div>{{caption}}</div>')($scope);});

然后像这样写HTML:

<div ng-bind-html="html"></div>

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

解决方案

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>

这篇关于AngularJS 数据绑定在 ng-bind-html 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆