Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a PrimeNG p-dialog box on a button click and pass data from the component to p-dialog?

I am working on an Angular migration project where the code is being refactored from AngularJS to Angular 5. Here is the AngularJS code snippet.

HTML

<ul >
    <li ng-if="participant.reports !== null && participant.reports !== undefined" ng-repeat="report in participant.reports">
        <a ng-click="choosePDFLang(participant, report.type, report.name)">
            <img src="i/pdf_image.png"/>{{ report.name | translate }}
        </a>
    </li>
</ul>

JS

$scope.choosePDFLang = function(participant, reportType, reportName)
            $modal.open({
                templateUrl: 'theTemplate'
                controller: 'theController',
                keyboard: true,
                backdrop: 'static',
                scope: $scope
            }).result.then(function() {
            });
        }

As you can see, when an item from the dropdown is clicked, it opened up a modal with its own template and controller which handled all the logic.

Now I have to apply the same logic using Angular 5. My project is using PrimeNG components. I have to use the dialog box <p-dialog></p-dialog>.

My question is : how do I open this dialog box and pass to it all the data when a report hyperlink is clicked ? In AngularJS I could do it easily by calling the $modal.open({}) function and giving it the same scope so now the modals controller has all the data as well.

like image 295
yugrac Avatar asked May 18 '18 17:05

yugrac


People also ask

What is P dialog in angular?

PrimeNG Issue Template is an Angular CLI. project used to provide a sample test case to. demonstrate a defect to help PrimeNG Team. ## Development server.

What is dismissableMask?

dismissableMask: It is used to specify if clicking the modal background should hide the dialog. It is of boolean data type, the default value is false.

What is baseZIndex?

baseZIndex(zIndex)The z-index value. The z-index of all overlay UI components located on a page is calculated based on the value passed to this method. Since an overlay UI component is added, its z-index is increased by one relative to a previously added overlay UI component.

How do I close PrimeNG dynamic dialog?

The primeng dialog has a two-way binding [(visible)] property that allows you to set a boolean value to it. By default, the value is false . So the same false flag can be used to close all your active dialogs when you perform some action (in your case its logout). Then, use the onClose event to catch it.


1 Answers

It's easier with Angular2+ and PrimeNG. You just need to define a display property to show or hide the p-dialog and you don't need to create another component.

Now, your code should look something like :

HTML

<ul>
    <li *ngFor="let report of participant.reports">
        <a (click)="choosePDFLang(participant, report.type, report.name)">
            {{ report.name }}
        </a>
    </li>
</ul>

<p-dialog header="{{modalTitle}}" [(visible)]="display">
    {{modalContent}}
</p-dialog>

TS

choosePDFLang(participant, reportType, reportName) {
    this.display = true;
    this.modalTitle = reportName;
    this.modalContent = 'Content of ' + reportName;
  }

See StackBlitz

Don't hesitate if you have any questions !

like image 161
Antikhippe Avatar answered Oct 20 '22 00:10

Antikhippe