如何在angularJs中基于API响应显示toastr

bejyjqdl  于 2023-10-15  发布在  Angular
关注(0)|答案(1)|浏览(97)

我正在应用程序中添加和编辑设备,并且希望根据API响应显示toastr

angular.module("myApp", ['ui.bootstrap', 'toastr']).controller("myController",   function ($scope, $http, $uibModal, toastr, $timeout) {

  `   if (newName) {
       $http.post('/api/Device/AddDevice', { name: newName })
         .then(function () {
             toastr.success("Device added","Sucess")
             $http.get('/api/Device/GetAllDevices')
                 .then(function (response) {
                     $scope.devices = response.data;
                 })
                 .catch(function (error) {
                     console.error('Error fetching devices:', error);
                 });
         })
}

PS:这不是全部代码

exdqitrt

exdqitrt1#

`Certainly, let's focus on the code responsible for displaying notifications using the PNotify library:`    
 <pre>
        <script type="text/javascript">
            var ngApp = angular.module('ngApp', ['ngFileUpload']);
            ngApp.controller('MainCtrl', ['$scope', 'Upload', '$http', '$log', '$filter', '$timeout', function ($scope, Upload, $http, $log, $filter, $timeout) {  
                $scope.model = { dtMemo: $filter("date")(Date.now(), 'dd-MMM-yyyy'), MemoAssignUsers: [] }
                $scope.InsertItem = function (filedata) {
                    if (filedata != 'Submit') {
                        $scope.filedata = filedata
                    }
                    else {
                        $scope.model.MemoAttachment = $scope.filedata;
                        Upload.upload({
                            url: '../Memo/prcDataSave',
                            data: { model: $scope.model }
                        }).then(function (response) {
                            console.log(response.data);
                            if (response.data == "1") {
                                new PNotify({
                                    title: 'Notification',
                                    text: 'Data Saved Successfully.',
                                    type: 'custom',
                                    addclass: 'notification-success',
                                    icon: 'fa fa-check'
                                });
                                $scope.model = {};
                                $scope.filedata = [];
        
                                $scope.model = { dtMemo: $filter("date")(Date.now(), 'dd-MMM-yyyy'),}
                            }
                            else {
                                new PNotify({
                                    title: 'Notification',
                                    text: '!! Failure : ' + response.data,
                                    type: 'custom',
                                    addclass: 'notification-danger',
                                    icon: 'fa fa-exclamation-triangle'
                                });
                            }
                        })
                    }
                }
            }]);
         
        </script>
        </pre>

相关问题