Create a simple Bootstrap Yes/No confirmation or just notification alert in AngularJS

后端 未结 5 1682
灰色年华
灰色年华 2020-12-02 18:49

It\'s so simple in a non-Angular environment. Just html and a two line of js code to show a modal confirmation dialog on the screen.

Now I am developting an AngularJ

5条回答
  •  抹茶落季
    2020-12-02 19:01

    For anything that has code that is triggered with a ng-click I just add a confirm attribute

    eg

    
    

    and confirm comes from (not mine, found on the web)

    app.controller('ConfirmModalController', function($scope, $modalInstance, data) {
            $scope.data = angular.copy(data);
    
            $scope.ok = function() {
                $modalInstance.close();
            };
    
            $scope.cancel = function() {
                $modalInstance.dismiss('cancel');
            };
        }).value('$confirmModalDefaults', {
            template: '',
            controller: 'ConfirmModalController'
        }).factory('$confirm', function($modal, $confirmModalDefaults) {
            return function(data, settings) {
                settings = angular.extend($confirmModalDefaults, (settings || {}));
                data = data || {};
    
                if ('templateUrl' in settings && 'template' in settings) {
                    delete settings.template;
                }
    
                settings.resolve = { data: function() { return data; } };
    
                return $modal.open(settings).result;
            };
        })
        .directive('confirm', function($confirm) {
            return {
                priority: 1,
                restrict: 'A',
                scope: {
                    confirmIf: "=",
                    ngClick: '&',
                    confirm: '@'
                },
                link: function(scope, element, attrs) {
                    function reBind(func) {
                        element.unbind("click").bind("click", function() {
                            func();
                        });
                    }
    
                    function bindConfirm() {
                        $confirm({ text: scope.confirm }).then(scope.ngClick);
                    }
    
                    if ('confirmIf' in attrs) {
                        scope.$watch('confirmIf', function(newVal) {
                            if (newVal) {
                                reBind(bindConfirm);
                            } else {
                                reBind(function() {
                                    scope.$apply(scope.ngClick);
                                });
                            }
                        });
                    } else {
                        reBind(bindConfirm);
                    }
                }
            }
        })
    

    My google FOO has failed me and I cannot find the source site for this. I will update if I find it.

提交回复
热议问题