【发布时间】: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