Comments (3)
Hi @mdstevens044,
There is 2 ways to use the modals right now, it all depends on your needs but for performance purpose I highly recommend using the service.
1st way: include mz-modal
component directly into your HTML template
This one is easy as the modal HTML is directly in your HTML template which means you can access all your values directly from your component.
example.component.html
<button mz-button (click)="modalExample.open()">Modal</button>
<p>
<b>inputValue:</b> {{ inputValue }}<br>
<b>agreed:</b> {{ agreed }}
</p>
<mz-modal #modalExample>
<mz-modal-header>
Modal Title
</mz-modal-header>
<mz-modal-content>
Modal Content
<mz-input-container>
<input mz-input
id="input"
[label]="inputLabel"
[(ngModel)]="inputValue"
type="text">
</mz-input-container>
</mz-modal-content>
<mz-modal-footer>
<button mz-button [flat]="true" mz-modal-close (click)="setModalAgreementValue(false)">Disagree</button>
<button mz-button [flat]="true" mz-modal-close (click)="setModalAgreementValue(true)">Agree</button>
</mz-modal-footer>
</mz-modal>
example.component.ts
import { Component } from '@angular/core';
@Component({...})
export class ExampleComponent {
public agreed: boolean;
public inputValue: string;
setModalAgreementValue(value: boolean) {
this.agreed = value;
}
}
2nd way: use the service MzModalService
to inject a component using the modal HTML
This one is a little tricky as you need to get the values from the returned component instance of the open
method of the service.
example.component.html
<button mz-button (click)="openServiceModal()">Modal Service</button>
<p>
<b>inputValue:</b> {{ modalComponentRef?.instance.inputValue }}<br>
<b>agreed:</b> {{ modalComponentRef?.instance.agreed }}
</p>
example.component.ts
import { Component, ComponentRef } from '@angular/core';
import { MzModalService } from 'ng2-materialize';
import { ModalExampleComponent } from './modal-example/modal-example.component';
@Component({...})
export class ExampleComponent {
public modalComponentRef: ComponentRef<ModalExampleComponent>;
constructor(
private modalService: MzModalService,
) { }
openServiceModal() {
// need to cast for now as the return type of MzModalService.open is MzBaseModal (this will be fix in a near futur)
this.modalComponentRef = <ComponentRef<ModalExampleComponent>>this.modalService.open(ModalExampleComponent);
}
}
modalExample.component.html
<mz-modal>
<mz-modal-header>
Modal Title
</mz-modal-header>
<mz-modal-content>
Modal Content
<mz-input-container>
<input mz-input
id="input"
[label]="inputLabel"
[(ngModel)]="inputValue"
type="text">
</mz-input-container>
</mz-modal-content>
<mz-modal-footer>
<button mz-button [flat]="true" mz-modal-close (click)="setModalAgreementValue(false)">Disagree</button>
<button mz-button [flat]="true" mz-modal-close (click)="setModalAgreementValue(true)">Agree</button>
</mz-modal-footer>
</mz-modal>
modalExample.component.ts
import { Component } from '@angular/core';
import { MzBaseModal } from 'ng2-materialize';
@Component({...})
export class ModalExampleComponent extends MzBaseModal {
public agreed: boolean;
public inputValue: string;
setModalAgreementValue(value: boolean) {
this.agreed = value;
}
}
You can see it in action on this pic ...
Hope it helps!
from ngx-materialize.
Awesome thanks.
from ngx-materialize.
More detail to help some guys want to get data in file ts (Typescript)
In Modal Component: Create a function to catch event of agree button (Ex: agreementEvent())
In Component using the modal component:
this.modalNameRef.instance.agreementEvent() { console.log(this.modalNameRef.instance.yourData); }
Done!
from ngx-materialize.
Related Issues (20)
- How to make a fixed navbar ? HOT 3
- Ho to align material-icon and a text-input HOT 1
- Tooltip with max width HOT 1
- Components no implements (click)
- autocomplete not working for input if data is dynamically bind through webservice HOT 3
- MzToastService.show: Materialize is not defined HOT 2
- Autocomplete not working with chip input (does with regular input) HOT 1
- Dropdown click inisde elements closes HOT 1
- Errors after updating to Angular 7 HOT 2
- Pagination component.
- Sidenav - this.collapseButton.sideNav is not a function HOT 5
- "Falling" icon in float+flat button in a mz-navbar HOT 1
- Chip selected when scrolling (swiping) through chip autocomplete list.
- Add own picture in select option with data-icon?
- Date picker and Time picker issue in latest chrome browser HOT 15
- Date picker - How Modify default Language HOT 2
- Text area not expanding on large input size HOT 1
- Getting compilation error when import ngx-materialize. HOT 1
- pagination breaks after totalItems = 0 HOT 1
- @angular/core has no exported member 'Renderer' HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-materialize.