Guide on implementing a modelpopup window with angularJS

Can someone please provide advice on how to display a model popup window with a button click in AngularJS, without using BootstrpJS?

I attempted the code below but encountered an issue. :(

html

    <div>
      <button ng-click='toggleModal()'>Add Dataset</button>
        <modal-dialog info='modalShown' show='modalShown' width='400px'     height='60%'>
            <p>Insert Modal Content Here</p>

  </modal-dialog>
    </div>

controller

app.controller('DataController', function ($scope,$http) {

    $scope.showModal = false;
    $scope.toggleModal = function () {
        $scope.showModal = !$scope.showModal;
    };


 $http.get("/api/product").then(function (responses) {
        $scope.ProductData = responses.data;
               });
.......
........
});

app.directive('modalDialog', function () {
    return {
        restrict: 'E',
        scope: {
            show: '=info'
        },
        replace: true, // Replace with the template below
        transclude: true, // we want to insert custom content inside the directive
        link: function (scope, element, attrs) {
            scope.dialogStyle = {};
            if (attrs.width)
                scope.dialogStyle.width = attrs.width;
            if (attrs.height)
                scope.dialogStyle.height = attrs.height;
            scope.hideModal = function () {
                scope.show = false;
            };
        },
        template: "<div class='ng-modal' ng-show='show'><div class='ng-modal-overlay' ng-click='hideModal()'></div><div class='ng-modal-dialog' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='hideModal()'>X</div><div class='ng-modal-dialog-content' ng-transclude></div></div></div>"
    };
});

Answer №1

It appears that you may be adjusting your scope too frequently. For a similar example, take a look at http://codepen.io/dboots/pen/vLeXPj. In this demonstration, I utilized the $scope.showModal variable and the $scope.toggleModal function to toggle visibility.

angular.module('testApp', [])
  .controller('DataController', function($scope) {
    $scope.showModal = false;
    $scope.toggleModal = function() {
      $scope.showModal = !$scope.showModal;
    };
  })
  .directive('modalDialog', function() {
    return {
      restrict: 'E',
      replace: true,
      transclude: true,
      link: function(scope, element, attrs) {
        scope.dialogStyle = {};
        if (attrs.width)
          scope.dialogStyle.width = attrs.width;
        if (attrs.height)
          scope.dialogStyle.height = attrs.height;
      },
      template: "<div class='ng-modal' ng-show='showModal'><div class='ng-modal-overlay' ng-click='toggleModal()'></div><div class='ng-modal-dialog' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='toggleModal()'>X</div><div class='ng-modal-dialog-content' ng-transclude></div></div></div>"
    
    };
  });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instead of modifying the selected class, jQuery generates inline styles

Utilizing the following code to dynamically create a class: $("head").append('<style type="text/css"></style>'); var newStyleElement = $("head").children(':last'); newStyleElement.html('.move{transform: translateX(1 ...

The v-for directive on a reactive Vuejs array is failing to render its elements

After much searching, I have identified the error and provided a brief explanation in the following paragraph. The code below resolves the error by changing the approach of the code rather than fixing the error itself. By defining the indexes of the array ...

lines stay unbroken in angular

I am encountering an issue when I execute the following code: DetailDisplayer(row) : String { let resultAsString = ""; console.log(row.metadata.questions.length); (row.metadata.questions.length != 0 )?resultAsString += "Questions ...

Elements on the page are being truncated beyond the visible viewport

Encountering an issue with a web page I'm currently developing that is proving challenging to explain. The problem occurs when the page is viewed in a small browser window and cuts off when scrolling horizontally to content outside of the viewport. Wh ...

Utilize jQuery and plotly.js to exclusively display newly selected items in a dropdown menu

I'm exploring plotly.js and I want to create a dynamic plot that changes based on dropdown selection. My code includes jQuery and bootstrap. However, I faced an issue where selecting a new option adds a new line to the existing graph instead of replac ...

Chrome is experiencing compatibility issues with Datatable

I'm encountering an issue with Datatables and I'm at a loss on how to resolve it. In my custom cms, Datatables functions perfectly on my Mac (Safari, Chrome, Firefox) and also works seamlessly for my colleagues on Windows machines. However, we ...

Assistance required: The database is not found upon page reload. How can this be resolved?

My HTML5 code has created a database with create, add, delete, and print operations. However, the ObjectStore is re-created every time I load the page, and the additional values stored are not present upon reloading the page. What could be wrong with my ...

CSS for Centering Text and ImageAchieving the perfect alignment

I have a footer positioned at the bottom of my webpage containing some text along with a few PNG images. I am attempting to center everything within the footer. I tried using margin: 0 auto, but it didn't work due to the footer not being a block elem ...

Adding style to the first element in an ng-repeat using ng-style in AngularJS

My issue involves applying a specific CSS style to only the first element in a set of styles. I attempted to use a custom directive for this purpose, but it did not achieve the desired result. I would greatly appreciate any assistance! Below is the relev ...

When using jQuery, the content loaded with the $ajax function only displays after refreshing the page

Located on an external server is a directory containing various .html files, including one named index.html. The server has the ability to load either the folder name or foldername/index.html in its URL. Each html file within the directory loads a corresp ...

Stop ui-router from transitioning based on an HTTP query

Is there a way to prevent a state change for a specific "to" state in ui-router without using onEnter? Let's consider the following route: .state('auth.confirm', { url: '/confirm/:resetToken', template: '<confirm-page& ...

Using YoutubeAPI v3 within AngularJS for Enhanced Services

Having used services before but still struggling with it, I've come across frequent issues. Therefore, I'm seeking assistance for what seems like an easy problem: retrieving ChannelDetails using YoutubeApi v3. This is my Service: appApi.factory ...

Choose all the alternative A radio buttons utilizing JavaScript

If the "4 stars" option is selected at the top, I want all corresponding "4 stars" options in the form to be automatically selected. Similarly, if "3 stars" is chosen, I need all the "3 stars" options to be selected. I attempted to achieve this functionali ...

What could be the reason for my ng-init not functioning properly?

<body ng-init="user=${userID};month=${month};curPageNum=${currentPage}"> Using JSP, I set initial values in the body tag. However, in the controller, I have the following code: console.debug($scope.user + " "+$scope.month} The issue is that only ...

I'm looking to create a Slider component using @material-ui/core that has a track divided into two sections with distinct colors for the left and right sides. How

When the slider ranges from -10 to 0, I want it to display in red. For values between 0 and +10, I would like it to be shown in green. Is there a way to implement this color change using the <Slider /> component from @material-ui/core? ...

The functionality of jQuery Revolution Slider becomes compromised when integrated with ember.js

Initially, the jQuery Revolution Slider on the website functions properly. However, when navigating to a different route and returning to the home/index page where the slider is located, it malfunctions. <script type='text/x-handlebars' data- ...

Why do all the select boxes require two clicks to open the drop-down menu when the function is activated?

After implementing this code on my webpage, I noticed an issue where all select boxes required two clicks from the user to open the drop-down menu. The first click seemed to set focus on the box, and only on the second click would the drop-down actually ap ...

Is there a more optimized CSS approach for this layout?

I attempted to create this layout using only CSS (no JavaScript, etc.) within an existing HTML page (without altering the HTML code). Unfortunately, I couldn't achieve all the positions that I needed. Let's see if anyone has a better idea. Thanks ...

Is it possible to permanently alter HTML values with JavaScript?

Can a html value be permanently modified using javascript? I am trying to edit a local file. Below are the codes I'm using: function switchPic(){ top.topPage.activeDef = top.topPage.document.getElementById('h1'); top.topPage.activeDef. ...

I would like the dropdown menu to only appear on mobile devices

After creating a dropdown menu, I now want it to only appear in mobile view and not in other views. I believe this can be achieved using "visible-xs" or a similar method, but I am struggling with the coding. Below is my HTML code: <nav class="navb ...