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

后端 未结 5 1680
灰色年华
灰色年华 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:10

    You can create a simple factory like this

    angular.module('app')
    .factory('modalService', [
        '$modal', function ($modal) {
            var self = this;
            var modalInstance = null;
            self.open = function (scope, path) {
                modalInstance = $modal.open({
                    templateUrl: path,
                    scope: scope
                });
            };
    
            self.close = function () {
                modalInstance.dismiss('close');
            };
            return self;
            }
    ]);
    

    In your controller

    angular.module('app').controller('yourController',  
      ['$scope','modalService',function($scope,modalService){
    
    $scope.openModal=function(){
     modalService.open($scope,'modal template path goes here');
     };
    
    $scope.closeModal=function(){
     modalService.close();
    //do something on modal close
     };
     }]);
    

    I have passed $scope in service function so that you can access closeModal function and in case you want to access some data from your controller . In your html

    
    

提交回复
热议问题