1

我正在尝试通过这个答案$interpolate使用并将ng-bind-html我的范围变量的数据绑定到一个 html 字符串。现在,当我的范围变量的值正在更新结果时,它不会更新。ng-bind-html

我不想$interpolate每次我的范围更新时都打电话。

这是我的控制器代码:

$scope.TitleFlag= true;
$scope.HtmlContent = "<div>{{TitleFlag}}</div>";
$scope.trustedHtml = $interpolate($scope.HtmlContent)($scope);
$scope.TitleFlagToggle = function(){
    $scope.TitleFlag= !$scope.TitleFlag;
  };

这是我的视图代码:

<div>{{TitleFlag}}</div> <!-- This is update correctly -->
<div ng-bind-html="trustedHtml"></div> <!-- This is not update -->
<button class="button" ng-click="TitleFlagToggle()"></button>
4

1 回答 1

0

While the TitleFlag is changing we need to interpolate it. So put the $scope.trustedHtml also in click function too.

$scope.TitleFlag= true;
$scope.HtmlContent = "<div>{{TitleFlag}}</div>";
$scope.trustedHtml = $interpolate($scope.HtmlContent)(  $scope);
$scope.TitleFlagToggle = function(){
  $scope.TitleFlag= !$scope.TitleFlag;
  $scope.trustedHtml = $interpolate($scope.HtmlContent)(  $scope);
};

http://plnkr.co/edit/rRjHOY2xSiAZ8EtzviqK?p=preview

于 2015-02-10T07:02:14.177 回答