【问题标题】:Angular provider - $animate is not working properlyAngular 提供程序 - $animate 无法正常工作
【发布时间】:2016-06-23 03:32:31
【问题描述】:

为什么这不起作用? $animate.leave 不知道要“离开”什么,导致我的新元素出现多个实例。为什么会这样?

ng-notify.js

angular.module('ngNotify', [])
    .provider('ngNotify', function () {

        this.$get = ['$injector', '$document', '$rootScope', function ($injector, $document, $rootScope) {

            this.show = function(){

                var $animate;

                if (!$animate) {
                    $animate = $injector.get('$animate');
                 }

                var bodyElement = angular.element(document.querySelector('body'))
                var redBlockElement = angular.element('<div class="red block"></div>')

                // close previous instances
                $animate.leave(redBlockElement);

                $animate.enter(redBlockElement, bodyElement).then(function() {                  
                    $rootScope.$apply();
                });

            }
            return this;
        }];

});

我将此代码作为单独的 JS 加载到我的主应用程序中。这就是我的app.js 文件的样子

var myApp = angular.module('myApp ',[
    'ui.router',
    'ngAnimate',
    'LocalStorageModule',
    'angularMoment',
    'angular-loading-bar',
    'angular.morris-chart',
    'mgo-angular-wizard',
    'myApp.version',
    'ngNotify', // the dependency I'm trying to create
])

**

更新

**

我把变量放在show() 之外,重复的问题就解决了。但是我怎样才能让我的消息动态呢?

'use strict';

angular.module('ngNotify', [])
.provider('ngNotify', function () {

    this.$get = ['$injector', '$document', '$timeout', '$rootScope', '$animate', function ($injector, $document, $timeout, $rootScope, $animate) {

        var html = '<div class="ng-notify">' + text + '</div>'; // text has to be dynamic

        var tmpl = angular.element(html);
        var body = $document.find('body').eq(0);
        var bodyElement = angular.element(document.querySelector('body'));

        this.show = function(text){
            $animate.enter(tmpl, body);
        }

        return this;
    }];

});

【问题讨论】:

  • 您发布的代码存在多个问题。这真的是您使用的代码吗?否则请出示。
  • 是的,这是我的全部代码。我应该提到我将此代码加载为单独的 JS 作为我的应用程序的依赖项。我添加了更多代码,如果可以,请检查一下。
  • 您没有注入$animate,并且$document 和$location 之间缺少一个逗号,这就是为什么我认为这不是您使用的代码。
  • 代码已编辑,仍然无法正常工作..
  • 你能描述一下代码应该做什么吗?是否存在要删除的 class="red block" 元素?如果存在多个,是否应该全部删除?

标签: angularjs jquery-animate provider


【解决方案1】:

问题是每次调用show,下面的代码都会创建一个新元素:

var redBlockElement = angular.element('<div class="red block"></div>')

这个元素还没有附加到 DOM,所以将它传递给 $animate.leave 不会有任何效果,因为没有什么可以删除。

然后将元素传递给$animate.enter 并附加到DOM。

所以每次调用show,都会创建并添加一个新元素。

一种解决方案是移动以下行,以便它们只被调用一次:

var bodyElement = angular.element(document.querySelector('body'));
var redBlockElement = angular.element('<div class="red block">Test</div>');

this.show = function() { ...

然后始终使用 redBlockElement 引用,将其传递给 leave、enter 或更改其文本值。

如果您希望它们每次都是不同的元素,您可以保留对先前创建的元素的引用并在创建和添加新元素之前将其删除,或者只需查询 DOM 并将其删除:

var bodyElement = angular.element(document.querySelector('body'));

this.show = function() {

  var existing = angular.element(bodyElement[0].querySelector('.red.block'));

  $animate.leave(existing);

  var redBlockElement = angular.element('<div class="red block"></div>');

  $animate.enter(redBlockElement, bodyElement).then(function() {
    $rootScope.$apply();
  });
};

请注意,此示例仅删除一个现有元素,即使有多个。

【讨论】:

  • 如何更改show() 中redBlockElement 的值? (请记住,show() 有一个参数,因此Test 基本上是动态文本。如果我再次声明redBlockElement,它将创建一个新元素并忽略我以前的元素。(这会导致重复的元素)
  • 这应该可以工作:redBlockElement.text(someVariable)
  • 声明 angular.element 必须在我理解的函数之外,否则它仍然会重复,看 - i.imgur.com/nXT6in7.png(完全使用你最近的代码)
  • 你是对的,天哪,这是我的错,对不起。哇,它的工作非常感谢你的耐心,即使我放弃了自己,你也没有放弃:(哈哈,非常感谢先生!
猜你喜欢
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多