Put an angular variable in an HTML attribute - html

Question:
In the below sample code, we can see that each message has a unique MessageSid.
In the HTML code, I would like to set
data-target="#deleteModal" to data-target="#{{message.MessageSid}}"
and similarly
id="deleteModal" to id="{{message.MessageSid}}"
How can I do this? I would like each modal to be individual to each message.
MessageDetail Model
export class MessageDetail {
MessageSid: string;
Body: string;
Time: string;
Direction: string;
FromPhoneNumber: string;
ToPhoneNumber: string;
}
HTML code
<div id="listOfMessages" class="mt-5">
<div class="container" *ngFor="let message of messageList" [ngClass]="classDirection(message)">
<img src="/assets/img/avatar.png" alt="Avatar">
<p>{{message.Body}}</p>
<button class="btn btn-danger btn-circle btn-circle-sm m-1 float-right" data-toggle="modal" data-target="#deleteModal"><i class="fa fa-times"></i></button>
<span class="time-right">{{message.TimeCreated}}</span>
<!-- Confirm Delete Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this message?</p>
<p>{{message.Body}}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" (click)="deleteMessage(message, $event)">Delete</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- End Confirm Delete Modal-->
</div>
</div>
I have tried this solution on my own. But it seems only Modals 1,4,7 in the list work. The ones in between do not pop up when clicked.
<div id="listOfMessages" class="mt-5">
<div class="container" *ngFor="let message of messageList" [ngClass]="classDirection(message)">
<img src="/assets/img/avatar.png" alt="Avatar">
<p>{{message.Body}}</p>
<button class="btn btn-danger btn-circle btn-circle-sm m-1 float-right" data-toggle="modal" attr.data-target="#{{message.MessageSid}}"><i class="fa fa-times"></i></button>
<span class="time-right">{{message.TimeCreated}}</span>
<!-- Confirm Delete Modal -->
<div class="modal fade" id="{{message.MessageSid}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this message?</p>
<p>{{message.Body}}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" (click)="deleteMessage(message, $event)">Delete</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- End Confirm Delete Modal-->
</div>
</div>

You should do in the HTML:
...
<div [id]="'#' + message.messageSId"> </div>
...
Inside the tag you should do with [attr]="typeScriptExpression" (property binding), not with attr={{typeScriptExpression}} (interpolation)
Reference to study: https://angular.io/guide/template-syntax

Thank you. I figured out the issue. Some MessageSids began with numbers and apparently you cannot have a tag with an ID that begins with a number. Prefixing a character solved the issue.

Related

In Angular, how do I resolve the modal not to be closed, when client clicks outside of it?

I've read several Q&A-s about it, all suggested a quite simple solution: to put a
data-backdrop="static" data-keyboard="false"
in the HTML tag of the button, which is responsible for the modal opening. For me, the button is:
<button type="button" class="btn btn-primary btn-danger m-2" data-bs-toggle="modal" data-bs-target="#exampleModal" data-backdrop="static" data-keyboard="false" (click)="onDelete(object.id)"><fa-icon [icon]="faTrash"></fa-icon></button>
and the complete modal is:
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<!-- <h5 class="modal-title text-center" id="exampleModalLabel">Biztosan törölni akarod?</h5> -->
<button type="button" #closeButton class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<h5 class="modal-title text-center" id="exampleModalLabel">Biztosan törölni akarod?</h5>
<p class="text-center">A törlés megerősítésével az adatbázisból is eltávolításra kerül a rekord.</p>
</div>
<div class="modal-footer justify-content-center">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal" (click)="confirmDelete()">Törlés</button>
<button type="button" class="btn btn-primary" data-bs-dismiss="modal" (click)="declineDelete()">Visszavonás</button>
</div>
</div>
</div>
</div>
but every time I press ESC, or click outside of the modal, it just closes. I also tried to manage it from TS, with a #ViewChild modal (by refering to the modal DOM div), and a dialog: MatDialog property, then, in the onDelete() method, which is called when pressing the Delete button, I simply typed this.modal.open(dialog, { disableClose: true });, it did not work either. I'd prefer to just resolve it from the HTML though. Did i miss something? (I'm using angular 14.1.1 and bootstrap v5)
Backdrop has to be set to static so the modal will not close when clicking outside it but it is set as data-bs-backdrop="static" instead of data-backdrop="static"
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">
Launch static backdrop modal
</button>
<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>

Why is my image not showing in the modal?

Hello everyone I am trying to make a modal which shows an image when a button is clicked. But I ran into a problem and dont seem to understand why my image is not showing in my modal. I am using bootstrap 4.5.2 to make this modal.
When I use this url inside of the src: //placehold.it/600x400. The image works fine but when I link the same image from my folder named resources the image does not load.
Here is my code
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="dynamic-content">
<img src="Resources/600x400.png" alt="officeMap" class="img-fluid">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Here is my path
Changing the file path to PlanPage/Resources/600x400.png solved the problem.

How do I make multiple MDB modals on the same page and have different content in each modal pop up?

How do I make multiple MDB modals on the same page and have different content in each modal pop up? Currently the content in the modal-body of the last modal will show up when either of the modals are clicked even though I have separate content in each modal-body? I used the MDB modal markup from here: https://mdbootstrap.com/docs/jquery/modals/generator/ Why is this?
Here is the my code:
<!-- Modal 1 -->
<button type="button" mdbBtn color="primary" class="relative waves-light p-2 modal-btns" (click)="basicModal.show()" mdbWavesEffect
*ngIf="showBasic">
Modal 1
</button>
<div mdbModal #basicModal="mdbModal" class="modal fade right" tabindex="-1" role="dialog"
aria-labelledby="myBasicModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close pull-right" aria-label="Close" (click)="basicModal.hide()">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title w-100" id="myModalLabel">Content 1</h4>
</div>
<div class="modal-body">
Content 1
</div>
<div class="modal-footer justify-content-center">
<button type="button" mdbBtn color="secondary" class="waves-light" aria-label="Close"
(click)="basicModal.hide()" mdbWavesEffect>Close</button>
<button type="button" mdbBtn color="primary" class="relative waves-light" mdbWavesEffect>OK!</button>
</div>
</div>
</div>
</div>
<!-- Modal 1 -->
<!-- Modal 2 -->
<button type="button" mdbBtn color="primary" class="relative waves-light p-2 modal-btns" (click)="basicModal.show()" mdbWavesEffect
*ngIf="showBasic">
Modal 2
</button>
<div mdbModal #basicModal="mdbModal" class="modal fade right" tabindex="-1" role="dialog"
aria-labelledby="myBasicModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close pull-right" aria-label="Close" (click)="basicModal.hide()">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title w-100" id="myModalLabel">Content 2</h4>
</div>
<div class="modal-body">
Content 2
</div>
<div class="modal-footer justify-content-center">
<button type="button" mdbBtn color="secondary" class="waves-light" aria-label="Close"
(click)="basicModal.hide()" mdbWavesEffect>Close</button>
<button type="button" mdbBtn color="primary" class="relative waves-light" mdbWavesEffect>OK!</button>
</div>
</div>
</div>
</div>
<!-- Modal 2 -->
#basicModal id is used in both modals. This value should be unique.
You need to change this value in the second modal code, for example:
<!-- Modal 2 -->
<button type="button" mdbBtn color="primary" class="relative waves-light p-2 modal-btns" (click)="secondModal.show()" mdbWavesEffect
*ngIf="showBasic">
Modal 2
</button>
<div mdbModal #secondModal="mdbModal" class="modal fade right" tabindex="-1" role="dialog"
aria-labelledby="myBasicModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close pull-right" aria-label="Close" (click)="basicModal.hide()">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title w-100" id="myModalLabel">Content 2</h4>
</div>
<div class="modal-body">
Content 2
</div>
<div class="modal-footer justify-content-center">
<button type="button" mdbBtn color="secondary" class="waves-light" aria-label="Close"
(click)="secondModal.hide()" mdbWavesEffect>Close</button>
<button type="button" mdbBtn color="primary" class="relative waves-light" mdbWavesEffect>OK!</button>
</div>
</div>
</div>
</div>
<!-- Modal 2 -->

Bootstrap 4 Modal Center Content

Can someone please explain how to horizontally center the title in a Bootstrap 4 modal.
I've tried text-center, mx-auto and ml-0 / mr-0 but they don't appear to work.
Here's a link to a fiddle.
Code below;
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
The modal-header is display:flex so centering its' content (like the modal-title) works differently. You can use mx-auto but then centering is relative to the close button so it's not exactly centered.
One option is to make the header display:block (d-block) and use text-center.
https://jsfiddle.net/44v0b25k/
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header d-block">
<button type="button" class="close float-right" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h5 class="modal-title text-center" id="exampleModalLabel">Modal title</h5>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Another option is to use w-100 on the modal-title so that it's full width, and text-center will also work.
<div class="modal-header">
<h5 class="modal-title w-100 text-center" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
https://jsfiddle.net/306ob2e5/
Below I have added text-center d-block to the header and d-inline-block to the title.
The problem was that the header was flex with justify-content space-between which pushed your title to the left.
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center d-block">
<h5 class="modal-title d-inline-block" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Example Bootply
Just apply CSS to the modal-title class, for example:
.modal-title {
text-align: center;
width: 100%;
}
Example: https://jsfiddle.net/vxsw0xsy/

Bootstrap 3 modal shows a transparent background

I created a simple HTML with bootstrap 3.2 :
<body>
<div>
<a class="btn" href="#frmLogin" data-toggle="modal">login</a>
</div>
<div class="modal fade" id="frmLogin" role="dialog">
<div class="modal-dialog">
<div class="modal-content>">
<div class="modal-header">
<a class="btn" data-dismiss="modal">×</a>
<h3 id="modalTitle">Login</h3>
</div>
</div>
</div>
</div>
</body>
but the modal does not show correctly.
fiddle link : http://jsfiddle.net/jsmLxhv9/1/
I'm a little late to the party here, but I had the same issue, which lead me to this post. The issue with the posted code is that
<div class="modal-content>">
needs to be
<div class="modal-content">
Other than that, it works fine. Check it out here: http://jsfiddle.net/hjqo17dq/
<body>
<!-- Button trigger modal -->
<button class="btn" data-toggle="modal" data-target="#myModal">
Login
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</body>