角度指令和范围

时间:2016-10-31 15:41:47

标签: angularjs angularjs-directive

我遇到以下问题。

我在视图中有以下代码

<a class="btn btn-hero" confirmation-download-all
ng-if="downloadPackageLink"><i class="fa fa-download" aria-hidden="true"></i> {{signDocumentDownloadAllButton}}</a>

我有一个指令

function confirmationDownloadAll(deviceDetector) {
    return {
        restrict : 'A',
        priority : 1,
        terminal : true,
        scope : {
            signDocumentDownloadAllButton : '=',
        },
        link : function (scope, element, attr) {
            var clickAction = attr.ngClick;
            element.bind('click', function (event) {
                event.preventDefault();
                if (scope.deviceDetector.os == 'ios') {
                    var sweetOptions = {
                        title : scope.signDocumentDownloadAllNotificationTitleIOS,
                        text : scope.signDocumentDownloadAllNotificationMessageIOS,
                        type : "warning",
                        showCancelButton : false,
                        confirmButtonColor : "#DD6B55",
                        confirmButtonText : scope.signDocumentDownloadAllButton,
                        closeOnConfirm : true
                    };
                    swal(sweetOptions,
                        function (isConfirm) {
                        if (isConfirm) {
                            var downloadall = document.getElementById('hidden_downloadall');
                            downloadall.click();
                            //if (sweetConfirmOption) swal(sweetConfirmOption);
                            //if (attrs.sweetOnConfirm) scope.$evalAsync(attrs.sweetOnConfirm);
                        }
                    });
                } else {
                    var downloadall = document.getElementById('hidden_downloadall');
                    downloadall.click();
                }
            });
        }
    };
}

我的问题是,在我看来,按钮中显示的文字是{{signDocumentDownloadAllButton}},而不是$scope. signDocumentDownloadAllButton

的值

我认为必须非常简单,但我开始贬低。

1 个答案:

答案 0 :(得分:-1)

在这里你去.......试试这个评论指明你的修改应该在哪里。

function confirmationDownloadAll(deviceDetector) {
return {
    restrict : 'A',
    priority : 1,
    terminal : true,
    scope : {
        signDocumentDownloadAllButton : '=',
    },
     // you missed the below line $scope and not just scope
    link : function ($scope, element, attr) {
        var clickAction = attr.ngClick;
        element.bind('click', function (event) {
            event.preventDefault();
            if ($scope.deviceDetector.os == 'ios') {
                var sweetOptions = {
                    title : $scope.signDocumentDownloadAllNotificationTitleIOS,
                    text : $scope.signDocumentDownloadAllNotificationMessageIOS,
                    type : "warning",
                    showCancelButton : false,
                    confirmButtonColor : "#DD6B55",
                    confirmButtonText : $scope.signDocumentDownloadAllButton,
                    closeOnConfirm : true
                };
                swal(sweetOptions,
                    function (isConfirm) {
                    if (isConfirm) {
                        var downloadall = document.getElementById('hidden_downloadall');
                        downloadall.click();
                        //if (sweetConfirmOption) swal(sweetConfirmOption);
                        //if (attrs.sweetOnConfirm) $scope.$evalAsync(attrs.sweetOnConfirm);
                    }
                });
            } else {
                var downloadall = document.getElementById('hidden_downloadall');
                downloadall.click();
            }
        });
    }
};
}
相关问题