如何在指令内访问 angularjs 中的控制器变量?

How to access controller variable in angularjs inside a directive?

我的 HTML 中有上传图片输入:

<div class="row">
    <div class="col-md-12">
        <div class="form-group"
            ng-class="(uploadSgnCtrl.showUploadSgnErrorMessage && uploadSgnCtrl.dataIsNullOrEmpty(uploadSgnCtrl.param)) ? 'has-error' : ''">
            <label for="signatureImage" class="required">Signature Image</label>
            <div class="input-group">
                <input type="file" id="file" name="signatureImage" 
                    class="form-control" data-file="uploadSgnCtrl.param"
                    ng-model="uploadSgnCtrl.param" file-upload>
            </div>
            <div class="row no-padding no-margin element-error-message">
                <ul class="no-padding no-margin">
                    <li ng-if="uploadSgnCtrl.dataIsNullOrEmpty(uploadSgnCtrl.param)"
                        class="no-padding no-margin">This field is Required</li>
                </ul>
            </div>
        </div>
    </div>
</div>

并且在我的指令中有以下代码:

module.directive('fileUpload', function() {
    return {
        scope : {
            file : '=',
            accept : '=',
            showUploadSgnErrorMessage : '=' //this doesn't work
        },
        link : function(scope, el, attrs, ctrl) {
            console.log(ctrl)
            el.bind('change',
                    function(event) {
                        var files = event.target.files;
                        var file = files[0];

                        var validFormats = [ 'jpg', 'jpeg', 'png', 'JPG',
                                'JPEG', 'PNG' ];

                        var value = file.name
                        var ext = value.substr(value.lastIndexOf('.') + 1);

                        if (ext == '')
                            return;

                        if (validFormats.indexOf(ext) !== -1
                                && file.size < 2097152) {
                            scope.file = file;
                            scope.$apply();
                        } else {
                            scope.file = null;
                            console.log('file more than 2mb'); //working

                            ctrl.showUploadSgnErrorMessage = true; //not working
                            scope.showUploadSgnErrorMessage = true; //not working
                            console.log(ctrl.showUploadSgnErrorMessage); //not working
                            console.log(scope.showUploadSgnErrorMessage); //not working
                            scope.$apply();
                        }

                    });
        }
    };
});

我想要什么,如果我上传一个超过 2mb 的文件,它将触发在我的 ng-class HTML 代码中设置的 "uploadSgnCtrl.showUploadSgnErrorMessage" 为真。但是上面的代码不起作用(请参阅注释代码)。尽管 console.log('file more than 2mb') 有效。我得到一个 TypeError: r is undefined 旁边。

首先将控制器变量绑定到指令范围,

<input type="file" id="file" name="signatureImage" 
    class="form-control" ng-model="uploadSgnCtrl.param"
    file="uploadSgnCtrl.param"
    accept=""
    show-upload-sgn-error-message="uploadSgnCtrl.showUploadSgnErrorMessage"
    file-upload>

然后使用范围变量并根据您的功能分配,

module.directive('fileUpload', function() {
    return {
        scope : {
            file : '=',
            accept : '=',
            showUploadSgnErrorMessage : '='
        },
        link: function() {
            ...
        }
    };
});

如果您只需要访问控制器范围并获取/设置一些值,您可以使用 $root :

scope.$root.showUploadSgnErrorMessage = true; 

我假设您的 uploadSgnCtrl 引用是父控制器并且它有一个 $scope