gpt4 book ai didi

javascript - 无法在mvc中的angular js bootstrap中使用超时

转载 作者:行者123 更新时间:2023-11-29 16:10:17 25 4
gpt4 key购买 nike

索引.cshtml

<!doctype html>
<html data-ng-app="ui.bootstrap.demo">
<head>

<script src="~/Scripts/angular.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.js"></script>
<script src="~/Scripts/example.js"></script>

<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div data-ng-controller="AlertDemoCtrl">
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</alert>
<button class='btn btn-default' ng-click="addAlert()">Add Alert</button>
</div>
</body>
</html>

例子.js

    angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('AlertDemoCtrl', function ($scope) {
$scope.alerts = [
{ type: 'danger', msg: 'Oh snap! Change a few things up and try submitting again.' },
{ type: 'success', msg: 'Well done! You successfully read this important alert message.' }
];

$scope.addAlert = function() {
$scope.alerts.push({msg: 'Another alert!'});
};

$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};

$timeout(function () {
$scope.alerts.splice($scope.alerts.indexOf(alert), 1);
}, 3000); // maybe '}, 3000, false);' to avoid calling apply

unable to close the alert after 3 sec interval, anything wrong ?

最佳答案

angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo')
.controller('AlertDemoCtrl', function ($scope, $timeout) { //here I've just injected the $timeout service to your controller. if your other codes are ok then this $timeout will work
$scope.alerts = [
{ type: 'danger', msg: 'Oh snap! Change a few things up and try submitting again.' },
{ type: 'success', msg: 'Well done! You successfully read this important alert message.' }
];

$scope.addAlert = function() {
$scope.alerts.push({msg: 'Another alert!'});
};

$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};

$timeout(function () {
$scope.alerts.splice($scope.alerts.indexOf(alert), 1); // you are using 'alert' to get the specific alert in alerts array of object, but you haven't passed/defined 'alert' anywhere. so how can you get this alert? and how can it be removed. Please pass/define 'alert', which is the portion you would know better. Then your code will work.
}, 3000); // maybe '}, 3000, false);' to avoid calling apply

关于javascript - 无法在mvc中的angular js bootstrap中使用超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30419833/

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