javascript - 无法使用 ngShow 为自定义指令设置动画

标签 javascript css angularjs twitter-bootstrap ng-animate

我正在尝试使用 ng-show 为自定义模式指令设置动画,遵循 Angular 上显示的示例网站(示例位于页面的最底部)。但是,我没有任何运气让它发挥作用。

我正在使用 Angular 1.3.x 和 Bootstrap 3.3.x。我没有使用 Angular UI Bootstrap,因为自定义模式指令更灵活地满足我的需求。

这是我到目前为止所得到的:

模态.js:

angular.module('plunker', [])
  .directive('myModal', function() {
    return {
      restrict: 'E',
      transclude: true,
      replace: true,
      templateUrl: 'modal.html',
      controller: 'ModalCtrl',
      link: function(scope, element, attrs) {
        scope.showModal = false;

        if (attrs.title === undefined) {
          scope.title = 'Modal Title Placeholder';
        } else {
          scope.title = attrs.title;
        }

        scope.$watch('showModal', function(isHidden) {
          if (isHidden) {

          } else {

          }
        });
      }
    };
  }).controller('ModalCtrl', ['$scope',
    function($scope) {
      $scope.$open = function() {
        $scope.showModal = true;
      };

      $scope.$close = function() {
        $scope.showModal = false;
      };
    }
  ]);

模态.html:

<div role="dialog" tabindex="-1" class="modal modal-fade" ng-show="showModal">
    <div class="modal-backdrop" style="height: 100%"> </div>
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title">{{title}}</h3>
            </div>
            <div class="modal-body">
                <div class="content" ng-transclude></div>
            </div>
        </div>
    </div>
</div>

模态.css:

.modal-fade {
  opacity: 1;
  display: block !important;
}

.modal-fade .ng-hide-add .ng-hide-add-active,
.modal-fade .ng-hide-remove .ng-hide-remove-active {
  -webkit-transition: all linear 1s;
  -moz-transition: all linear 1s;
  transition: all linear 1s;
}

.modal-fade .ng-hide {
  opacity: 0;
  display: none;
}

index.html:

<my-modal title="Example Modal">
    <p>Some text</p>
    <button type="button" class="btn btn-primary" ng-click="$close()">Close</button>
</my-modal>
<button type="button" class="btn btn-primary" ng-click="$open()">Open Modal</button>

其他一切正常,即模式显示并可以关闭,但没有动画。

这是一个 Plunkr代码链接

最佳答案

AngularJS 的动画 Hook 是在一个名为 ngAnimate 的单独模块中实现的.

加载脚本:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.11/angular-animate.min.js"></script>

并添加模块:

angular.module('plunker', ['ngAnimate'])

其次,您需要连接 CSS 类以形成正确的选择器。

代替:

.modal-fade .ng-hide {

做:

.modal-fade.ng-hide {

第一个是后代选择器,它将选择类为 ng-hide 的元素。并且是类别为 modal-fade 的元素的后代.

第二个将选择具有这两个类的元素,这是您需要的。

对于这种情况,您只需要:

.modal-fade {
  -webkit-transition: all linear 1s;
  -moz-transition: all linear 1s;
  transition: all linear 1s;
  display: block;
}
.modal-fade.ng-hide {
  opacity: 0;
}

display: block只需要覆盖 display: none来自 modal Bootstrap 中的类。

演示: http://plnkr.co/edit/h0tNOn6Xj31OGmHqulr5?p=preview

关于javascript - 无法使用 ngShow 为自定义指令设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28465442/

相关文章:

javascript - 如何动态创建不同的 mousedown 回调?

javascript - 如何防止 Highcharts 中出现气泡?

ruby-on-rails - Angular + .nghaml 模板 : Referencing Precompiled Image Assets

javascript - 使用 React Native 异步返回一个值

javascript - 未捕获的类型错误 : $(. ..).fullCalendar 不是 Laravel 5 中的函数

javascript - 填充输入后自动添加一个div

javascript - 有没有一种方法可以让我们通过 makeStyles 在 Material-UI 中使用属性选择器?

javascript - 在多态性中使用父类(super class)中子类原型(prototype)中定义的变量

javascript - 回调中的Node.js错误处理(xml-stream)

angularjs - 带有复选框的 Angular 过滤器