与AngularJS范围变量一起使用时,Html不会在引导程序弹出窗口中呈现

时间:2015-10-15 12:14:49

标签: angularjs twitter-bootstrap

在这个小提琴中:

http://jsfiddle.net/RLQhh/2695/

如果点击"打开模态"文本"这是一个新的行"出现在两行上。

然而如果使用这个小提琴:

http://jsfiddle.net/RLQhh/2694/

文字"this is a <br> new line"出现在一行上。与范围变量一起使用时,<br>元素不会呈现为html。

我在两个例子中都设置了data-html="true"

为什么会这样?如何在使用范围变量应用时呈现html?

小提琴src:

<div ng-controller="MainCtrl" class="container">
  <h1>Modal example</h1>
  <button ng-click="toggleModal()" class="btn btn-default">Open modal</button>

  <modal title="Login form" visible="showModal">
    <form role="form">
      <div class="form-group">
        <label data-html="true">{{text}}</label>

      </div>
      <button type="submit" class="btn btn-default">Submit</button>
    </form>
  </modal>
</div>

var mymodal = angular.module('mymodal', []);

mymodal.controller('MainCtrl', function ($scope) {
    $scope.showModal = false;
    $scope.text = "this is a <br> new line";
    $scope.toggleModal = function(){
        $scope.showModal = !$scope.showModal;
    };
  });

mymodal.directive('modal', function () {
    return {
      template: '<div class="modal fade">' + 
          '<div class="modal-dialog">' + 
            '<div class="modal-content">' + 
              '<div class="modal-header">' + 
                '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' + 
                '<h4 class="modal-title">{{ title }}</h4>' + 
              '</div>' + 
              '<div class="modal-body" ng-transclude></div>' + 
            '</div>' + 
          '</div>' + 
        '</div>',
      restrict: 'E',
      transclude: true,
      replace:true,
      scope:true,
      link: function postLink(scope, element, attrs) {
        scope.title = attrs.title;

        scope.$watch(attrs.visible, function(value){
          if(value == true)
            $(element).modal('show');
          else
            $(element).modal('hide');
        });

        $(element).on('shown.bs.modal', function(){
          scope.$apply(function(){
            scope.$parent[attrs.visible] = true;
          });
        });

        $(element).on('hidden.bs.modal', function(){
          scope.$apply(function(){
            scope.$parent[attrs.visible] = false;
          });
        });
      }
    };
  });

0 个答案:

没有答案