Html 有没有具体的方法可以在5秒后用角度元素移除数组元素
我正在寻找开发一个系统上使用角一通知消失的特点。以下是我的HTML代码:Html 有没有具体的方法可以在5秒后用角度元素移除数组元素,html,arrays,angularjs,Html,Arrays,Angularjs,我正在寻找开发一个系统上使用角一通知消失的特点。以下是我的HTML代码: <div ng-app="myApp" ng-controller="myCtrl"> <button ng-click='makeNotification("New Notification")'>Add</button> <ul> <li ng-repeat='n in notifications'>{{n.notification}}<
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click='makeNotification("New Notification")'>Add</button>
<ul>
<li ng-repeat='n in notifications'>{{n.notification}}</li>
</ul>
</div>
这和我预期的一样。但这似乎是代码中的错误。某些通知保留,不删除
还有别的好办法吗?下面是我创建的JSFIDLE。
您可以使用烤面包机来显示通知,而不是手动编码。这很简单。烤面包机将在规定的时间后自动清除信息 请按照以下链接选择角形烤面包机: 或 (可选):要与bower一起安装,请使用:
bower install --save angularjs-toaster
或使用npm:
npm install --save angularjs-toaster
或使用链接脚本:
<link href="https://cdnjs.cloudflare.com/ajax/libs/angularjs-toaster/1.1.0/toaster.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js" ></script>
<script src="https://code.angularjs.org/1.2.0/angular-animate.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angularjs-toaster/1.1.0/toaster.min.js"></script>
单击按钮时调用控制器方法:
<div ng-controller="myController">
<button ng-click="pop()">Show a Toaster</button>
</div>
展示烤面包机
您可以使用烤面包机来显示通知,而不是手动对其进行编码。这很简单。烤面包机将在规定的时间后自动清除信息
请按照以下链接选择角形烤面包机:
或
(可选):要与bower一起安装,请使用:
bower install --save angularjs-toaster
或使用npm:
npm install --save angularjs-toaster
或使用链接脚本:
<link href="https://cdnjs.cloudflare.com/ajax/libs/angularjs-toaster/1.1.0/toaster.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js" ></script>
<script src="https://code.angularjs.org/1.2.0/angular-animate.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angularjs-toaster/1.1.0/toaster.min.js"></script>
单击按钮时调用控制器方法:
<div ng-controller="myController">
<button ng-click="pop()">Show a Toaster</button>
</div>
展示烤面包机
显示通知的最佳方式是使用任何第三方模块,如toastr或notify。但是,如果您想创建自己的,则可以销毁ul
HTML
另外,我还没有对此进行测试。显示通知的最佳方式是使用任何第三方模块,如toastr或notify。但是,如果您想创建自己的,则可以销毁
ul
HTML
另外,我还没有对此进行测试。您可能会发现,如果在几秒钟内添加了多个通知,则会有一些通知无法删除,因为
索引
指向错误的位置
而不是跟踪通知在数组中的位置,
您必须跟踪通知本身
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
$scope.notifications = [];
$scope.makeNotification = function(notification) {
var index = $scope.notifications.length;
// need to keep track of this rather than the index it's added at!
var notificationObject = { notification: notification + " " + index };
$scope.notifications.push(notificationObject);
//preserve context by wrapping the function that will be called by $timeout
var timeoutFunc = (function(obj){
return function() {
var index = $scope.notifications.indexOf(obj)
if (index >= 0)
$scope.notifications.splice(index, 1);
};
})(notificationObject);
$timeout(timeoutFunc, 5000);
}
});
或
正如其他人所指出的,您可以只删除数组中的第一个项,这要简单得多。您可能会发现,如果在几秒钟内添加了多个通知,则会有一些通知不会被删除,因为
索引
指向错误的位置
而不是跟踪通知在数组中的位置,
您必须跟踪通知本身
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
$scope.notifications = [];
$scope.makeNotification = function(notification) {
var index = $scope.notifications.length;
// need to keep track of this rather than the index it's added at!
var notificationObject = { notification: notification + " " + index };
$scope.notifications.push(notificationObject);
//preserve context by wrapping the function that will be called by $timeout
var timeoutFunc = (function(obj){
return function() {
var index = $scope.notifications.indexOf(obj)
if (index >= 0)
$scope.notifications.splice(index, 1);
};
})(notificationObject);
$timeout(timeoutFunc, 5000);
}
});
或
正如其他人所指出的那样,您可以删除数组中的第一个项,这要简单得多。只需尝试在超时函数中为remove元素使用单独的索引即可
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
$scope.notifications = [];
$scope.makeNotification = function(notification) {
var index = $scope.notifications.length;
$scope.notifications.push({notification:notification + " " + index});
$timeout(function() {
var Deleteindex = $scope.notifications.length-1;
$scope.notifications.splice(Deleteindex, 1);
}, 5000);
}
});
只需尝试在超时函数中为remove元素使用单独的索引
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
$scope.notifications = [];
$scope.makeNotification = function(notification) {
var index = $scope.notifications.length;
$scope.notifications.push({notification:notification + " " + index});
$timeout(function() {
var Deleteindex = $scope.notifications.length-1;
$scope.notifications.splice(Deleteindex, 1);
}, 5000);
}
});
索引中可能存在的错误?当您从数组中删除(拼接)元素时,数组中的项目会减少,因此您需要减少
索引
,除了超时
有点困难。它不应该是$scope.notifications.splice(0,1)代码>?要删除所有通知还是仅删除最后一个通知?可能存在索引错误?当您从数组中删除(拼接)元素时,数组中的项目会减少,因此您需要减少索引
,除了超时
有点困难。它不应该是$scope.notifications.splice(0,1)代码>?要删除所有通知还是仅删除最后一个通知?这非常有用!:)我会试试这个。但如果您能为我的解决方案提供建议,我将不胜感激。这真的很有帮助!:)我会试试这个。但如果你能为我的解决方案提供建议,我真的很感激。谢谢你,老兄!经过一些修改,你的建议就可以使用了。但这不是我想要的。我将使用第三方插件进行通知。但任何人都可以给出一个解决方案,这将有助于达到学习目的。谢谢老兄!经过一些修改,你的建议就可以使用了。但这不是我想要的。我将使用第三方插件进行通知。但任何人都可以给出一个解决方案,这将有助于达到学习目的。像魅力一样工作!谢谢你,伙计,祝你圣诞快乐D@Niroshan没问题。祝你圣诞快乐!谢谢你,伙计,祝你圣诞快乐D@Niroshan没问题。圣诞快乐