sac Dahal
sac Dahal

Reputation: 1241

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

.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}}

Right now I am using angular expressions {{}} to show error. But I would like to pop up a model in case of error containing the response.data.error. Is it possible to have a model whose value will be true if the server sends error response. Anyway what will be the best way to show errors using controllers?

Upvotes: 3

Views: 180

Answers (1)

Alec BW
Alec BW

Reputation: 243

You can use angular-ui-notification to show notifications. Below is Github link

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'});
});

Upvotes: 1

Related Questions