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.
var newMessage = $scope.onOpen(); if (angular.isString(newMessage)) $scope.message = newMessage; // in case the messages changed