我尝试使用ng-show设置自定义模式指令的动画,遵循Angular网站上显示的示例(示例位于页面的最底部)。但是,我没有运气好运。
我使用Angular 1.3.x和Bootstrap 3.3.x.我没有使用Angular UI Bootstrap,因为自定义模态指令对我的需求更灵活。
这是我到目前为止所得到的:
Modal.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;
};
}
]);
modal.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>
modal.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链接。
答案 0 :(得分:1)
AngularJS'动画钩子在一个名为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
覆盖Bootstrap中display: none
类的modal
。