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

How to display toastr based on API response in angularJs

提问人:J.trytocode 提问时间:9/2/2023 最后编辑:Brian Tompsett - 汤莱恩J.trytocode 更新时间:10/10/2023 访问量:30

问:

我正在我的应用程序中添加和编辑设备,我想根据 API 响应显示烤面包机

    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:这不是全部代码

HTML AngularJS 的

评论

0赞 ggorlen 9/18/2023
调用不应该在回调中吗?.success()then

答:

-1赞 Md Somrat Akbor 10/10/2023 #1
   `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>