Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Change message text in directive
Message
 
 
À
Tous
Information générale
Forum:
Javascript
Catégorie:
Autre
Titre:
Change message text in directive
Divers
Thread ID:
01611384
Message ID:
01611384
Vues:
42
Hi everybody,

I have the following directive:
app.directive('confirmButton', ['resourceFactory', function (resourceFactory) {
        return {
            restrict: 'AE',
            transclude: true,
            replace: true,
            scope: {
                linkText: '@',
                okLabel: '@',
                cancelLabel: '@?',
                confirmTitle: '@',
                confirmMessage: '=',
                onOpen: '&',
                okAction: '&',
                disableAction: '='
            },
            controller: ['$scope', '$modal', function ($scope, $modal) {
                $scope.okClass = 'btn-danger';
                $scope.message = $scope.confirmMessage;
                $scope.title = $scope.confirmTitle;
                
                $scope.cancelLabel = $scope.cancelLabel || resourceFactory.getResource('Labels', 'cancel');
                $scope.click = function (event) {             
                 
                        if ($scope.onOpen) {
                            $scope.onOpen();
                            $scope.message = $scope.confirmMessage; // in case the messages and title have changed
                            $scope.title = $scope.confirmTitle;
                        }
                        var modal = $modal.open({
                            scope: $scope,
                            templateUrl: '/app/templates/modal'
                        });

                        modal.result.then(function (result) {
                            $scope.okAction();
                        });
                    };              
            }],
            template: '<a ng-click="click()" ng-disabled="disableAction" >{{ linkText }}</a>'
        };
    }]);
And this is how I call it from the ng-form:
 <data-confirm:button class="btn btn-default" confirm-title="@Labels.confirmPayInvoices"
                                 confirm-message="payConfirmationMessage"
                                 ok-label="@Labels.yes"
                                 cancel-label="@Labels.no"
                                 link-text="@String.Format(Labels.payX, Labels.invoices)"
                                 disable-action="total('payment')==0"
                                 on-open="setConfirmPayInvoicesMessage()"
                                 ok-action="payInvoices()"></data-confirm:button>
The problem here is the confirmMessage. In the init of the form I set it to the empty string and in the setConfirmPayInvoicesMessage I set this variable to a new text, e.g.
if (nInvoicesToPay > 0) {

                    if (nInvoicesToPay === 1)
                        message = resourceFactory.getResource('Messages', 'confirmPayInvoice').format(totalPayment);
                    else
                        message = resourceFactory.getResource('Messages', 'confirmPayInvoices').format(totalPayment, nInvoicesToPay);
                }
                
                    $scope.payConfirmationMessage = message;
When I trace the code in the Developer's Tools, I can see that this message is set correctly in the controller's code, but in the directive onOpen message it still remains the same blank message. I tried using $scope.$apply function, but it produced an error message that apply is already in progress.

So, if I click on my button, the message remains blank in the new modal dialog. If I click on that button again, the message is correct.

What should I do to make that message update in the directive when I change it in my controller's code?

Thanks a lot in advance.

UPDATE. This is how I solved the problem for now, although it's not very nice solution:
var newMessage = $scope.onOpen();
                            if (angular.isString(newMessage))
                                 $scope.message = newMessage; // in case the messages changed
If it's not broken, fix it until it is.


My Blog
Répondre
Fil
Voir

Click here to load this message in the networking platform