在NG-结合HTML的AngularJS的数据绑定? [英] AngularJS data bind in ng-bind-html?

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

问题描述

是否有可能范围的变量的数据绑定到一个HTML是要作为约束NG-绑定-HTML?

Is it possible to bind data of scope variable to a html that is about to bind as ng-bind-html?

也就是说,我有一个

html ="<div>{{caption}}</div>";

和我的角模板的样子,

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

范围变量的值标题设在角控制器。

所以,我想在数据绑定{{标题}}

在此先感谢..

推荐答案

您应该使用$不插编译$。结果
写控制器是这样的:

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);
});

然后写HTML是这样的:

Then write HTML like this:

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

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

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