简体   繁体   English

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

[英]How to access controller variable in angularjs inside a directive?

I have a upload image input in my HTML :我的 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>

And in my directive a have the below code:在我的指令中有以下代码:

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

                    });
        }
    };
});

What i want if i upload a file that is more than 2mb it will trigger the "uploadSgnCtrl.showUploadSgnErrorMessage" to true that is set in my ng-class HTML code.我想要什么,如果我上传一个超过 2mb 的文件,它将触发“uploadSgnCtrl.showUploadSgnErrorMessage”为真,这是在我的 ng-class HTML 代码中设置的。 But above code not working(please see commented code).但上面的代码不起作用(请参阅注释代码)。 Though console.log('file more than 2mb') works.虽然 console.log('file more than 2mb') 有效。 I get a TypeError: r is undefined next to it.我收到一个 TypeError: r is undefined 在它旁边。

Bind controller variables to directive scope first,首先将控制器变量绑定到指令范围,

<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>

Then use scope variables and assign according to your functionality,然后使用范围变量并根据您的功能进行分配,

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

If you just need to access the controller scope and get / set somes value you can use $root :如果您只需要访问控制器范围并获取/设置 somes 值,您可以使用$root

scope.$root.showUploadSgnErrorMessage = true; 

I assume your uploadSgnCtrl reference is the parent controller and it has a $scope .我假设您的uploadSgnCtrl引用是父控制器并且它有一个$scope

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

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