我认为最好的方法是使用$animateProvider.classNameFilter
which 将允许您过滤项目以进行动画处理,或者在这种情况下不进行动画处理。我们会做类似的事情:
$animateProvider.classNameFilter(/^((?!(fa-spinner)).)*$/);
//$animateProvider.classNameFilter(/^((?!(fa-spinner|class2|class3)).)*$/);
演示:
http://plnkr.co/edit/lbqviQ87MQoZWeXplax1?p=preview
角度文档状态:
设置和/或返回执行动画时检查的 CSS 类正则表达式。在引导时,classNameFilter 值根本没有设置,因此将使 $animate 尝试在任何元素上执行动画。设置 classNameFilter 值时,只会对成功匹配过滤器表达式的元素执行动画。这反过来又可以提高低功率设备以及包含大量结构操作的应用程序的性能。
作为指令注释的另一个答案no-animate
,您可以编写一个ng-show
指令,该指令将以更高的优先级运行并禁用动画。只有当元素具有类时,我们才会这样做fa-spinner
。
problemApp.directive('ngShow', function($compile, $animate) {
return {
priority: 1000,
link: function(scope, element, attrs) {
if (element.hasClass('fa-spinner')) {
// we could add no-animate and $compile per
// http://stackoverflow.com/questions/23879654/angularjs-exclude-certain-elements-from-animations?rq=1
// or we can just include that no-animate directive's code here
$animate.enabled(false, element)
scope.$watch(function() {
$animate.enabled(false, element)
})
}
}
}
});
演示: http ://plnkr.co/edit/BYrhEompZAF5RKxU7ifJ?p=preview
最后,与上面类似,no-animate
如果我们想让它更加模块化,我们可以使用该指令。在这种情况下,我将命名faSpin
您可以在上面的答案中执行的指令。这基本上是相同的,只是我们保留了上述代码集注释中提到的 SO 答案中的指令。如果您只关心以fa-spin
这种方式命名的动画问题,则效果很好,但如果您有其他ng-show
动画问题,您希望将其命名ngShow
并添加到 if 子句中。
problemApp.directive('noAnimate', ['$animate',
function($animate) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
$animate.enabled(false, element)
scope.$watch(function() {
$animate.enabled(false, element)
})
}
};
}
])
problemApp.directive('faSpin', function($compile, $animate) {
return {
priority: 1000,
link: function(scope, element, attrs) {
if (element.hasClass('fa-spinner')) {
element.attr('no-animate', true);
$compile(element)(scope);
}
}
}
});
演示: http ://plnkr.co/edit/P3R74mUR27QUyxMFcyf4?p=preview