gpt4 book ai didi

javascript - ng-show 的 Angular 初始转换,使用 $timeout 和 $compile

转载 作者:太空宇宙 更新时间:2023-11-04 08:21:39 25 4
gpt4 key购买 nike

我有一项服务使用 $compile 将一些 HTML 附加到 DOM。此 HTML 包含一个 ng-show,带有用于显示和隐藏的转换,并且它以 falsy 条件开始。问题是当它附加到 DOM 时,它会显示隐藏过渡。我怎样才能避免它?我注意到我的 Controller 在 $timeout 内调用此服务,然后才会发生不希望的转换。

https://plnkr.co/edit/og0wrp6rZ65StXbufqLI?p=preview

angular
.module('app', ['ngAnimate'])
.directive('compileNgShowAnimation', ($timeout, $compile) => ({
restrict: 'E',
link: (scope, element) => {
$timeout(() => {
angular
.element(element)
.append($compile(`
<div>
Show:
<input type="checkbox" ng-model="checked" aria-label="Toggle ngShow">
<br />
<div
class="check-element animate-show-hide"
ng-show="checked">
I should show up only when the checkbox is checked.
</div>
</div>
`)(scope))
})
}
}))
.animate-show-hide.ng-hide {
/*transform: translate3d(0,-100%, 0);*/
opacity: 0;
}

.animate-show-hide:not(.ng-hide) {
/*transform: translate3d(0,0, 0);*/
opacity: 1;
}

.animate-show-hide.ng-hide-add,
.animate-show-hide.ng-hide-remove {
transition: all linear 0.5s;
}

.check-element {
border: 1px solid black;
padding: 10px;
}

/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.min.js"></script>
<body ng-app="app">

<compile-ng-show-animation></compile-ng-show-animation>

</body>

最佳答案

一个选项是通过将 ng-hide 类添加到 div 来确保该元素默认隐藏:

<div class="check-element animate-show-hide ng-hide" ng-show="checked">
I should show up only when the checkbox is checked.
</div>

那你就不会得到短暂的可见度

https://plnkr.co/edit/Ik3BflLHwu3DXglOMOhX?p=preview

关于javascript - ng-show 的 Angular 初始转换,使用 $timeout 和 $compile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45487334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com