{"id":11981,"date":"2025-02-06T05:46:35","date_gmt":"2025-02-06T05:46:35","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11981"},"modified":"2025-02-06T05:46:35","modified_gmt":"2025-02-06T05:46:35","slug":"simple-popup-by-using-angular-js","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/angular\/simple-popup-by-using-angular-js","title":{"rendered":"Simple Popup by Using Angular JS"},"content":{"rendered":"<p>Creating modal popups is a common requirement in AngularJS applications. These modals are often used for providing feedback to users or confirming actions dynamically. This guide explains how to implement a modal popup in AngularJS that displays a custom message based on the button clicked.<\/p>\n<h2>Problem Overview<\/h2>\n<p>A common requirement in AngularJS applications is to show a modal popup with a message like:<\/p>\n<ul>\n<li>&#8220;You clicked the Cancel button&#8221;<\/li>\n<li>&#8220;You clicked the Deny button&#8221;<\/li>\n<\/ul>\n<h2>Challenges include<\/h2>\n<ul>\n<li>Dynamically setting messages based on button actions.<\/li>\n<li>Ensuring a clean, reusable design for the modal popup.<\/li>\n<\/ul>\n<h3>Solution: Simple Modal Popup in AngularJS<\/h3>\n<p>This solution uses a straightforward AngularJS controller to manage modal visibility and message content, paired with a simple HTML structure for the modal.<\/p>\n<h3>Example<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">&lt;head&gt;\r\n&lt;style&gt;\r\n    .modal-overlay {\r\n      position: fixed;\r\n      top: 0;\r\n      left: 0;\r\n      width: 100%;\r\n      height: 100%;\r\n      background: rgba(0, 0, 0, 0.5);\r\n      display: flex;\r\n      justify-content: center;\r\n      align-items: center;\r\n    }\r\n    .modal-content {\r\n      background: #fff;\r\n      padding: 20px;\r\n      border-radius: 5px;\r\n      text-align: center;\r\n      width: 300px;\r\n    }\r\n    .modal-header {\r\n      font-weight: bold;\r\n      margin-bottom: 10px;\r\n    }\r\n    .modal-buttons {\r\n      margin-top: 15px;\r\n    }\r\n  &lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body ng-controller=\"MainController\"&gt; \r\n  &lt;button ng-click=\"showModal('Cancel')\"&gt;Cancel&lt;\/button&gt;\r\n  &lt;button ng-click=\"showModal('Deny')\"&gt;Deny&lt;\/button&gt;\r\n  &lt;button ng-click=\"showModal('Success')\"&gt;Success&lt;\/button&gt;\r\n  &lt;button ng-click=\"showModal('Remove')\"&gt;Remove&lt;\/button&gt;\r\n\r\n  &lt;custom-modal\r\n    show=\"isModalVisible\"\r\n    message=\"modalMessage\"\r\n    on-close=\"closeModal()\"&gt;\r\n  &lt;\/custom-modal&gt;\r\n\r\n&lt;script&gt; \r\nangular.module('modalApp', [])\r\n  .controller('MainController', ['$scope', function ($scope) {\r\n    $scope.isModalVisible = false;\r\n    $scope.modalMessage = '';\r\n\r\n    $scope.showModal = function (buttonName) {\r\n      $scope.modalMessage = `You clicked on ${buttonName} button`;\r\n      $scope.isModalVisible = true;\r\n    };\r\n\r\n    $scope.closeModal = function () {\r\n      $scope.isModalVisible = false;\r\n    };\r\n  }])\r\n  .directive('customModal', function () {\r\n    return {\r\n      restrict: 'E',\r\n      scope: {\r\n        show: '=',\r\n        message: '=',\r\n        onClose: '&amp;'\r\n      },\r\n      template: `\r\n        &lt;div class=\"modal-overlay\" ng-show=\"show\"&gt;\r\n          &lt;div class=\"modal-content\"&gt;\r\n            &lt;div class=\"modal-header\"&gt;Modal Popup&lt;\/div&gt;\r\n            &lt;div class=\"modal-body\"&gt;\r\n              &lt;p&gt;{{message}}&lt;\/p&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=\"modal-buttons\"&gt;\r\n              &lt;button ng-click=\"close()\"&gt;Close&lt;\/button&gt;\r\n            &lt;\/div&gt;\r\n          &lt;\/div&gt;\r\n        &lt;\/div&gt;\r\n      `,\r\n      link: function (scope) {\r\n        scope.close = function () {\r\n          scope.onClose();\r\n        };\r\n      }\r\n    };\r\n  });\r\n&lt;\/script&gt; \r\n&lt;\/body&gt;\r\n<\/pre>\n<h2>How It Works<\/h2>\n<h3>1. Controller Logic:<\/h3>\n<p>The MainController manages the visibility of the modal (isModalVisible) and the message (modalMessage).<\/p>\n<p>The showModal function sets the appropriate message based on the button clicked and makes the modal visible.<\/p>\n<h3>2. Directive:<\/h3>\n<p>The customModal directive creates a reusable modal component.<br \/>\nIt uses an ng-show directive to toggle visibility and onClose to handle the close action.<\/p>\n<h3>3. Styling:<\/h3>\n<p>Basic styles are added to make the modal look centered and visually distinct.<\/p>\n<p>When you click any button, the corresponding message will appear in the modal, and you can close it by clicking the &#8220;Close&#8221; button.<\/p>\n<h2>Conclusion<\/h2>\n<p>Using this approach, you can implement a dynamic modal popup in AngularJS with minimal effort. It\u2019s an effective solution for scenarios requiring context-sensitive feedback to user actions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating modal popups is a common requirement in AngularJS applications. These modals are often used for providing feedback to users or confirming actions dynamically. This guide explains how to implement a modal popup in AngularJS that displays a custom message based on the button clicked. Problem Overview A common requirement in AngularJS applications is to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11983,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-11981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11981"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11981"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11981\/revisions"}],"predecessor-version":[{"id":11984,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11981\/revisions\/11984"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11983"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}