javascript - AngularJS:将 $scope 传递给指令

标签 javascript angularjs angularjs-directive jquery-file-upload

尝试使用 jQuery 文件上传插件和指令要点创建文件上传指令 https://gist.github.com/thoughtpalette/4726114

我需要将 profile-ctrl.js 中对象的 ID 传递到上传表单的提交功能中。

我的指令位于 Controller 之外,我无法将对象设置为 $rootScope 以在指令中捕获/传递它。

url: in fileupload 是魔法发生的地方。通过 api 调用我的 Controller 并传递文件信息,并希望传递 profile-ctrl.js 中商家对象中的 vendorId。 $scope.merchant

不幸的是,由于项目结构,我不能在 fiddle 中发帖

我的指令:

//jqueryFileUpload-Plugin 
//https://github.com/blueimp/jQuery-File-Upload
define(['jquery',
        'angular',
        'core',
        'source/core/helpers/urlHelper.js'],
    function ($, angular, core, urlHelper) {
    "use strict";
    return [ function ($compile) {
        return {
            restrict:'E',
            scope: {
                merchant: '=ngModel'
            },
            compile:function(el,attrs, scope){
                var compiler = this,
                elem = el,
                vendorId = scope.merchant.id;
                instanceFn = function() {
                    var currentScope = this,
                        fileInputDiv = $(' <span class="btn btn-success fileinput-button">'+
                        '<i class="icon-plus icon-white"></i>'+
                        '<span>Upload Logo</span>'+
                        '<input type="file" name="files[]" single>'+
                        '</span>').appendTo(elem),
                        fileInput = fileInputDiv.find('input'),
                        fileList = $('<div class="UploaderList">'+
                        '<table>'+
                        '<tr>'+
                        '<td>File to Upload</td>'+
                        '</tr>'+
                        '</table>'+
                        '</div>').appendTo(elem),
                        button = $('<button class="btn">Submit</button>').appendTo(elem);

                    button.hide();
                    $('<div class="uploader">').appendTo(elem);
                    $('</div>').appendTo(elem);

                    fileInput.fileupload({
                        url:urlHelper.vendor.uploadLogo(vendorId),
                        dataType: 'json',
                        add:function (e, data) {

                            button.show();

                            // this will add a handler which will submit this specific file
                            button.bind('click.uploadsubmit', function(){ 
                                data.submit();
                            });
                            $.each(data.files, function (index, file) {
                                $("<tr><td>"+file.name+"</td><td></td><tr>").appendTo(fileList.find('table:first'));
                            });
                        },
                        // for each file
                        done: function (e, data) {

                            button.hide();

                            var result = "",
                                r = data.result,
                                file = data.files[0]; // we only support single file upload by now
                            // error

                            // CHANGE THIS PART FOR YOUR REQUIREMENTS (I have a json repsonse)

                            if(r.success !== undefined && r.success === false){
                                result = "<span class='error'>Unknown error</span>";
                                if(r.errors !== undefined && r.errors.length > 0 && r.errors[0].message !== undefined)
                                {
                                    result = "<span class='error'>"+r.errors[0].message+"</span>";
                                }
                            }
                            else{
                                result = "<span class='success'>OK</span>";
                            }

                            $("td:contains('"+file.name+"')").next().html(result);
                        },
                        progressall:function (e, data) {
                            var progress = parseInt(data.loaded / data.total * 100, 10);
                        },
                        // called only once... (wen submit button is clicked)
                        start:function(e){

                            // we start the upload, so we do also cleanup jobs right now:
                            button.unbind('click.uploadsubmit'); // importan - remove all event handlers
                            button.hide();
                            fileInputDiv.hide();

                        }
                    });

                };
                return instanceFn;
            }

        };
    }]; 
});

通过 html 的指令调用:

<jquery-File-Upload ng-model="merchant"></jquery-File-Upload>

所以现在商家在指令中返回未定义。有人可以帮我传递吗?

最佳答案

如评论中所述,编译函数的第三个参数不是作用域,它是一个嵌入链接函数。这是 an example了解如何使用它,如果您好奇的话。

您定义的 instanceFn 是您的链接函数,因为它由编译函数返回。链接函数的第一个参数是作用域,merchant 应该定义在它上面。

尝试 $watch()ing merchant,然后在发现变化后执行您的逻辑。

关于javascript - AngularJS:将 $scope 传递给指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14739895/

相关文章:

javascript 如何比较日期时间

javascript - 顶线填充 Nvd3 图表

angularjs - 添加新模块在 angularjs 中不起作用

javascript - 带有 $http 请求的 AngularJS 指令(inprog 错误)

javascript - 使用 join() 批量处理数组?

javascript - 如何使用 Html Agility Pack 的实际源代码获取 javascript 代码

javascript - React 和 Webpack 模块解析失败 :/testimonials. js 意外 token (6:4)

javascript - AngularJS:在编译前获取指令的html内容

javascript - 需要在状态退出时关闭模态

用于查询服务器返回 "Unsatisfied requests"的指令的 AngularJS 单元测试