Adding a conditional to target a bootstrap modal in AngularJS

I am facing a situation where I need a modal to only appear when specific conditions defined in my controller are met, rather than every time a button is clicked.

Here's the HTML CODE:

<button class="btn btn-primary-outline" type="button" data-uk-modal="{target:'#previewModal'
}" ng-click="previewOfferBefore()">Preview</button>

The above code successfully displays the modal with the id 'previewModal' when clicked. My plan is to incorporate the conditional logic within the controller and use Angular data binding to dynamically set the value of "target".

For example:

<button class="btn btn-primary-outline" type="button" data-uk-modal="{target: {{ previewLink 
}}}" ng-click="previewOfferBefore()">Preview </button>

In the controller, I would have:

$scope.previewOfferBefore = function() {
  if (/*some conditions here*/) {
    $scope.previewLink = '#'; /*prevent the modal from popping up */
  }
  else {
    $scope.previewLink = '#previewModal' /*allow the modal to pop up */
  }
}

I also experimented with using ng-href instead of Bootstrap's data-uk-modal, but that did not yield the desired outcome. I am confident in the functionality of my controller function because when I output {{ previewLink }} within a paragraph tag, it displays the correct id as expected. Therefore, the issue lies in how I am binding the data inside the button class.

Answer №1

In the scenario where you are comfortable with the button being inactive or shaded, an effective approach would involve utilizing ng-disabled. Your controller function would resemble the following snippet;

$scope.previewOfferBefore = function() {
if (/*insert relevant conditions here*/) {
     $scope.canClick= true;
 }
else {
     $scope.canClick= false;
 }
}

Subsequently, your HTML code would be updated as follows;

 <button ng-disabled="canClick" class="btn btn-primary-outline" type="button" data-uk-modal="{target:'#previewModal'
 }" ng-click="previewOfferBefore()">Preview</button>

As a result, the button will be rendered non-operational if it satisfies the false condition within the if statement.

Answer №2

Consider having two separate buttons and utilize ng-if or ng-show to display the appropriate one based on your expression. This can easily be achieved using Angular.

<button ng-show="previewLink !== '#'" type="button" data-uk-modal="{target: '#previewLink'}" ng-click="previewOfferBefore()">
    Preview
</button>

<button ng-show=""previewLink === '#'"" type="button" data-uk-modal="{target: '#'" ng-click="previewOfferBefore()">
    Preview
</button>

Alternatively:

You may want to explore using ng-attr to insert your Angular expression into the attribute like

ng-attr-uk-modal="{{'target:' + value}}"

Then, bind the value to the target you require. Keep in mind that this method might require some adjustments.

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

Can an image map be utilized within an <a> element?

I am attempting to implement an image map within an <a> tag. It seems to be functioning correctly in Chrome, but I am encountering issues with it not working in Internet Explorer. Is it acceptable to use an image map in an <a> tag? Below is th ...

The NODE.JS application becomes unresponsive when attempting to retrieve 2.5 million records from an API

I'm facing an issue where my app is retrieving millions of rows from a database and API, causing it to get stuck after calling the getData() function. let closedOrdersStartDate; preparedOrdersPromise = tickApiConnector.obtainT ...

Implementing a personalized image picker for CKeditor5 image insertion in a React JS environment

My goal is to integrate CKeditor into a custom document editor, and while using the ckeditor5-react module made it easy to integrate the state into the data, the default behavior for inserting an image does not align with my specific use case. I have an im ...

Having trouble displaying images in my 360-degree rotation slider

I am completely new to java script, so I have been copying and pasting code. However, it seems like the images are not loading properly. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" ...

Adjust parent div size based on image size increase

I am currently facing a situation where I have a page displaying an image, but sometimes it appears too small. In order to make the image larger, I have utilized CSS Transform and it is working well. However, the issue lies in the fact that the parent DIV ...

Are there any JavaScript alternatives to Python's pass statement that simply performs no action?

Can anyone help me find a JavaScript alternative to the Python: pass statement that does not implement the function of the ... notation? Is there a similar feature in JavaScript? ...

What is the reason for jQuery returning an integer instead of an object?

Currently, I am attempting to preselect an option within a <select> tag based on a variable. However, while using jQuery to select the elements and iterating through them with .each(), it appears to be returning integers instead of objects, making .v ...

Is there a way to adjust the navigator locale on a mobile device?

I'm currently testing out a utility function I created for displaying timestamps in a chat. My goal is to make it accessible worldwide and have it dynamically adjust based on the user's language settings. However, I've run into some issues ...

Working with Angular to Retrieve a File Using Ajax

My dilemma revolves around a service that is capable of generating a CSV file and sending it back to the page through an http/ajax get request. The desired user interaction involves clicking a button, triggering the service call, and initiating the downloa ...

The socket context provider seems to be malfunctioning within the component

One day, I decided to create a new context file called socket.tsx: import React, { createContext } from "react"; import { io, Socket } from "socket.io-client"; const socket = io("http://localhost:3000", { reconnectionDela ...

Is there a way to compress my JavaScript and CSS files using gzip?

I am facing an issue with gzipping a prototype library. I have no idea how to go about it, where to start, and how it actually works. I tried looking at some tutorials but unfortunately, they weren't very helpful... So here's the setup: I have ...

Utilizing CSS Grid to create a modern layout design featuring a fullwidth header and footer,

I have a traditional website layout with a header, main content area with a sidebar, and a footer. I want the header and footer to span the entire width on wider screens, while the main content and sidebar are fixed width, surrounded by whitespace. When th ...

When attempting to print using React, inline styles may not be effective

<div styleName="item" key={index} style={{ backgroundColor: color[index] }}> The hex color code stored in color[index] displays correctly in web browsers, but fails to work in print preview mode. Substituting 'blue' for color[index] succe ...

Styling triangles within a CSS triangle

I'm attempting to design a webpage with a fixed triangle navigation element. The issue I am encountering is that I am unable to position smaller triangles inside the larger one, as shown in the image below. https://i.stack.imgur.com/1bTj8.png As th ...

Does the server transmit HTML page content rather than the information you need?

My React application is connected to a backend built with Node.js. However, when I try to fetch data in my React component, the server sends back an HTML page instead of the expected data, displaying raw HTML on the screen. Backend (server.js): app.route ...

Comparing the use of .on in a directive versus in a controller

When deciding between attaching a directive to an element or binding an event inside the controller, what is considered best practice? Directive <openread-more what-to-expand="teds-bets-readmore" /> myApp.directive('openreadMore', functi ...

How can Socket.io prevent other pages from receiving all messages?

I apologize for the confusing title, but I am in need of some assistance in clarifying my question. The situation is as follows: I have a website page that is receiving messages from a node server. socket.on('item finished', function(data){ ...

Hey there, I'm looking to use different CSS fonts on Windows and Mac for the same page on a web application. Can someone please guide me on how to accomplish this?

In order to tailor the font based on the operating system, the following criteria should be followed: For Windows: "Segoe UI" For Mac: "SF Pro" Attempts have been made using the code provided below, but it seems to load before the DOM and lacks persisten ...

Generating a new root in React 18 results in numerous rounds of rendering and execution

Every time I attempt to run this code in React 18, it seems to render multiple times unlike React 17 where it only executes once and functions properly. How can I modify the code so that it only runs once? Is there a specific reason for the multiple execu ...

Having trouble with React image rendering using webpack?

Struggling to display an image in my React Web App, I encountered a broken image despite the correct file path appearing in the console log. We're utilizing url-loader and file-loader with webpack, importing the file path directly as necessary. Attemp ...