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

时间:2018-08-20 05:54:20

标签: angularjs html5 angularjs-directive angularjs-scope angularjs-ng-repeat

我在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”为true。但是上面的代码不起作用(请参阅注释的代码)。虽然console.log('文件超过2mb')有效。我收到TypeError:r在它旁边是未定义的。

2 个答案:

答案 0 :(得分:1)

首先将控制器变量绑定到指令作用域,

<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() {
            ...
        }
    };
});

答案 1 :(得分:1)

如果您只需要访问控制器作用域并获取/设置somes值,则可以使用$root

scope.$root.showUploadSgnErrorMessage = true; 

我假设您的uploadSgnCtrl引用是父控制器,并且具有$scope