简体   繁体   English

角材料-$ mdDialog自定义模板-ng-click指令中的函数?

[英]Angular Material - $mdDialog custom template - ng-click for function in directive?

I've used $mdDialog.show() within a custom directive with a templateUrl property to access a custom template for the the dialog itself, in which I've included two buttons; 我在带有templateUrl属性的自定义指令中使用了$ mdDialog.show()来访问对话框本身的自定义模板,其中包括了两个按钮。 a 'Cancel' button and a 'Confirm' button. “取消”按钮和“确认”按钮。

For each of these I've added an ng-click with calls to 'cancelLeaver()' and 'confirmLeaver()' functions, which I need to be able to write in the directive, but have no idea how I would do. 对于每一种方法,我都添加了ng-click,并带有对“ cancelLeaver()”和“ confirmLeaver()”函数的调用,我需要能够在指令中编写这些信息,但不知道该怎么做。

I've attempted to use the $mdDialog.then() functionality, but this didn't work because of having buttons in a custom view template. 我尝试使用$ mdDialog.then()功能,但是由于自定义视图模板中包含按钮,因此无法使用。

How can I call functions from a view template's ng-click attribute that are written inside a separate directive? 如何从视图模板的ng-click属性中调用写在单独指令中的函数?

HTML: HTML:

<md-dialog ng-controller="mainController" style="min-width: 30vw">

<md-dialog-content>

    <h2 class="md-title">Confirm leaver status</h2>

</md-dialog-content>

<md-dialog-content class="minus-padding-top">

    <p>Enter employee's leave date below</p>

    <br>

    <md-input-container class="md-prompt-input-container">

        <input id="leaveDate" name="leaveDate" ng-model="employee.leaveDate" ng-init="employee.leaveDate = currentDate" aria-label="Leave Date" required>

    </md-input-container>

</md-dialog-content>

<md-dialog-actions>

    <md-button ng-click="cancelLeaver()">Cancel</md-button>

    <md-button ng-click="confirmLeaver()" ng-disabled="!employee.leaveDate.length">Confirm</md-button>

</md-dialog-actions>

</md-dialog>

Directive: 指示:

app.directive('makeLeaver', function($window, $mdDialog, $mdToast, $timeout, $state) {
return {
    restrict: 'A',
    scope: {
        employee: '=',
    },
    controller: 'employeeDetailsController', 
    controllerAs: 'employeeDetails',
    bindToController: true,
    link: function(scope, element, attrs) {
        element.bind('click', function() {

            console.log('makeLeaver(' + '#' + scope.employee.id + ')');

            /* Show confirmation prompt dialog */
            $mdDialog.show({
                parent: angular.element('body'),
                clickOutsideToClose: true,
                templateUrl: 'views/employees/employeeDetails/dialogs/makeLeaver.html',
                targetEvent: element
            })

            /* ---------------- TRIED THIS BUT DIDN'T WORK ---------------- */
            scope.cancelLeaver = function() {
                console.log('cancelLeaver()');
                $mdDialog.hide();
            }

            scope.confirmLeaver = function() {
                console.log('confirmlLeaver()');
            }

        })
    }
}
})

You can use $rootScope.$broadcast('foo') and then receive it elsewher like $rootScope.on('foo'). 您可以使用$ rootScope。$ broadcast('foo'),然后像$ rootScope.on('foo')一样接收它。 another way would be to pass the scope through the function. 另一种方法是将范围传递给函数。 Check out this tutorial . 查看本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM