AngularJS:将$ scope传递给Directive

时间:2013-02-06 22:13:43

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

尝试使用jQuery File Upload插件和指令https://gist.github.com/thoughtpalette/4726114

的这个要点创建文件上传指令

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

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

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

遗憾的是,由于项目结构,我无法发布信息

我的指示:

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

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

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,编译函数的第三个参数不是范围,它是一个转换链接函数。如果你很好奇,这里有an example如何使用它。

您定义的instanceFn是您的链接函数,因为它是由compile函数返回的。链接函数的第一个参数是范围,应在其上定义merchant

尝试$ watch()ing merchant,然后在发现更改后执行逻辑。