什么'当从AngularJS中的API响应接收到错误时,触发错误弹出模式的最佳方式

What's the best to way to trigger an error pop up modal when receive an error from API response in AngularJS?

本文关键字:错误 方式 最佳 模式 当从 AngularJS 中的 响应 API 什么      更新时间:2023-09-26

.success(function (data) {
                console.log(data);
                $http.put("/admin/" + response.data._id + "/facilities", {
                    update: { $push: { facilities: data.facility._id } }
                }).success(function (data) {
                    $window.location.href='/admin/registrationSuccess';
                });
            });
            }, function (response) {
                $scope.list = response.data.error;
            });
 .form-group.col-lg-7
               label(for='username') Username:
               input.form-control(type='text', ng-model = 'username', placeholder='Enter                  Username', required='')
               p {{list}}

现在我正在使用角度表达式{{}}来显示错误。但我想在出现包含response.data.error的错误时弹出一个模型。如果服务器发送错误响应,是否可能有一个值为true的模型。无论如何,使用控制器显示错误的最佳方式是什么?

您可以使用angular ui通知来显示通知。下面是Github链接

https://github.com/alexcrack/angular-ui-notification

// Simple PUT request example:
var req = {
    method: 'PUT',
    url: '/someUrl',
    headers: {
        'Content-Type': 'application/json'
    },
    data: { test: 'test' }
}
$http(req).then(function successCallback(response) {
    // Here you can process successful response 
    // Show notification
    Notification.success({message: 'Success notification'});
}, function errorCallback(response) {
    // Here you can process error
    // Show notification
    Notification.error({message: 'Error notification'});
});