我不确定您是否可以覆盖浏览器对警报的处理。您至少需要使用注入的工厂来更改(查找和替换)您的警报代码......
这样做的好处是,您可以将 modalFactory 注入您需要从(可能是所有这些)引发模式的任何控制器中,并一致地重用该代码。虽然它没有回答您覆盖 Alert 的问题,但它确实为您提供了一种以编程方式显示模态元素的方法。这是 plunk http://plnkr.co/edit/0QNxUrQ7DH101VrFD0Me?p=preview希望它有所帮助。
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, modalFactory) {
$scope.name = 'ttt';
$scope.RunProcess = function() {
//error occured
modalFactory.show($scope);
}
$scope.Reset = function() {
modalFactory.hide($scope);
}
});
app.factory('modalFactory', function() {
return {
show: function($scope) {
$scope.showModal = true;
},
hide: function($scope) {
$scope.showModal = true;
}
}
});
那么在你看来
<body ng-controller="MainCtrl">
<div class="my-modal" ng-show="showModal">
blahhh
</div>
<p>Hello {{name}}!</p>
<div>
<button ng-click="RunProcess()">button that runs a controller method and throws alert</button>
</div>
</body>