总是从最后一个模态html获取标题的指令

时间:2015-09-03 14:09:19

标签: angularjs bootstrap-modal

我有以下方式的代码

'use strict';
 angular.module('abc.directives', [])
.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">{{ modalTitle }}</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.modalTitle = 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;
          });
        });
      }
    };  
  });

和html一样

<modal title="success" id='successmessage' visible="successmessage">
    <form role="form">
        Action was uccesful
        <span class="clearfix"></span>
        <button type="submit" class="btn btn-primary" ng-click="closemodal('successmessage');>Ok</button>
    </form>
</modal>    
<modal title="Error" id='errormessage' visible="errormessage">
<form role="form">
    Action was not Succesful
    <span class="clearfix"></span>
    <button type="submit" class="btn btn-primary" ng-click="closemodal('errormessage');>Ok</button>
</form>

所有功能都可以。但在模态标题中,标题始终显示为

Error 

即。最后一个模态窗口的标题。即使我打开成功窗口同样的情况。我该如何解决这个问题?

以下是JSfiddle链接:http://jsfiddle.net/98v0pozz/4/

2 个答案:

答案 0 :(得分:1)

您已将成功模式的标题设置为&#39;错误&#39;。

试试这个:

<modal title="Success" id='successmessage' visible="successmessage">
    <form role="form">
        Action was uccesful
        <span class="clearfix"></span>
        <button type="submit" class="btn btn-primary" ng-click="closemodal('successmessage');>Ok</button>
    </form>
</modal>    
<modal title="Error" id='errormessage' visible="errormessage">

答案 1 :(得分:1)

将“id”添加到模态标签:

<modal id="0" title="Login form" visible="showModal">
<modal id="1" title="Login form1" visible="showModal1">

在控制器中:

$scope.titles = [];
$scope.toggleModal = function(){
    $scope.showModal = !$scope.showModal;
    $scope.title = $scope.titles["0"];
};
 $scope.toggleModal1 = function(){
    $scope.showModal1 = !$scope.showModal1;
    $scope.title = $scope.titles["1"];
};

指令:

scope.titles[attrs.id] = attrs.title;