Git Product home page Git Product logo

Comments (3)

jfcere avatar jfcere commented on June 4, 2024 1

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 ...

modal-in-template

Hope it helps!

from ngx-materialize.

mdstevens044 avatar mdstevens044 commented on June 4, 2024

Awesome thanks.

from ngx-materialize.

vuongquang avatar vuongquang commented on June 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.