Javascript 如果在指令中定义了事件,如何获取触发该事件的元素

Javascript 如果在指令中定义了事件,如何获取触发该事件的元素,javascript,jquery,angularjs,angularjs-directive,Javascript,Jquery,Angularjs,Angularjs Directive,以下是来自内置指令(blueimp)的行 有更好的方法吗?这似乎还可以。通常,您不会尝试在控制器内引用dom元素,这可能是因为该元素不容易使用。控制器应该只在模型上工作。@Chandermani谢谢你的回答,你认为$(“#myfilefield”)是MVC的一个好用法吗?我有一个想法,在js控制器中使用html模板中的一些ID不是很好,但我不确定。您认为除了获取元素的漫长道路和通过$(“#myfilefield”)获取元素之外,还有获取字段类型对象的方法吗?thanksDirectives可以在

以下是来自内置指令(blueimp)的行


有更好的方法吗?

这似乎还可以。通常,您不会尝试在控制器内引用dom元素,这可能是因为该元素不容易使用。控制器应该只在模型上工作。@Chandermani谢谢你的回答,你认为$(“#myfilefield”)是MVC的一个好用法吗?我有一个想法,在js控制器中使用html模板中的一些ID不是很好,但我不确定。您认为除了获取元素的漫长道路和通过$(“#myfilefield”)获取元素之外,还有获取字段类型对象的方法吗?thanksDirectives可以在DOM上工作,也可以操作DOM。我说的是控制器。获取控制器中的元素时,您希望执行什么操作?@Chandermani$('#fileupload').fileupload({add:function(e,data){console.log('add');data.submit();},progress:function(e,data){console.log('progress');},start:function(e){console.log('start');}我需要在template controller中编写类似的内容,但我不想编写$('#fileupload'),而是编写$scope.fileuuplad或类似的内容,我认为这可能更具MVCial性。
.controller('FileUploadController', [
            '$scope', '$element', '$attrs', '$window', 'fileUpload',
            function ($scope, $element, $attrs, $window, fileUpload) {
                alert('incontroller');

                var uploadMethods = {
                    progress: function () {
                        return $element.fileupload('progress');
                    },
                    active: function () {
                        return $element.fileupload('active');
                    },
                    option: function (option, data) {
                        return $element.fileupload('option', option, data);
                    },
                    add: function (data) {
                        return $element.fileupload('add', data);
                    },
                    send: function (data) {
                        return $element.fileupload('send', data);
                    },
                    process: function (data) {
                        return $element.fileupload('process', data);
                    },
                    processing: function (data) {
                        return $element.fileupload('processing', data);
                    }
                };
                $scope.disabled = !$window.jQuery.support.fileInput;
                $scope.queue = $scope.queue || [];
                $scope.clear = function (files) {
                    var queue = this.queue,
                        i = queue.length,
                        file = files,
                        length = 1;
                    if (angular.isArray(files)) {
                        file = files[0];
                        length = files.length;
                    }
                    while (i) {
                        i -= 1;
                        if (queue[i] === file) {
                            return queue.splice(i, length);
                        }
                    }
                };
                $scope.replace = function (oldFiles, newFiles) {
                    var queue = this.queue,
                        file = oldFiles[0],
                        i,
                        j;
                    for (i = 0; i < queue.length; i += 1) {
                        if (queue[i] === file) {
                            for (j = 0; j < newFiles.length; j += 1) {
                                queue[i + j] = newFiles[j];
                            }
                            return;
                        }
                    }
                };
                $scope.applyOnQueue = function (method) {
                    var list = this.queue.slice(0),
                        i,
                        file;
                    for (i = 0; i < list.length; i += 1) {
                        file = list[i];
                        if (file[method]) {
                            file[method]();
                        }
                    }
                };
                $scope.submit = function () {
                    this.applyOnQueue('$submit');
                };
                $scope.cancel = function () {
                    this.applyOnQueue('$cancel');
                };
                // Add upload methods to the scope:
                angular.extend($scope, uploadMethods);
                // The fileupload widget will initialize with
                // the options provided via "data-"-parameters,
                // as well as those given via options object:
                $element.fileupload(angular.extend(
                    {scope: function () {
                        return $scope;
                    }},
                    fileUpload.defaults
                )).on('fileuploadadd', function (e, data) {
                    data.scope = $scope.option('scope');
                }).on('fileuploadchange', function (e, data) {
                    data.scope = $scope.option('scope');
                    data.scope.extend({
                        mycustomfield:$element
//i added above line, is it illegal, or too ugly to get element,or needless,some other way?
                    });
                }).on([
                    'fileuploadadd',
                    'fileuploadsubmit',
                    'fileuploadsend',
                    'fileuploaddone',
                    'fileuploadfail',
                    'fileuploadalways',
                    'fileuploadprogress',
                    'fileuploadprogressall',
                    'fileuploadstart',
                    'fileuploadstop',
                    'fileuploadchange',
                    'fileuploadpaste',
                    'fileuploaddrop',
                    'fileuploaddragover',
                    'fileuploadchunksend',
                    'fileuploadchunkdone',
                    'fileuploadchunkfail',
                    'fileuploadchunkalways',
                    'fileuploadprocessstart',
                    'fileuploadprocess',
                    'fileuploadprocessdone',
                    'fileuploadprocessfail',
                    'fileuploadprocessalways',
                    'fileuploadprocessstop'
                ].join(' '), function (e, data) {
                    if ($scope.$emit(e.type, data).defaultPrevented) {
                        e.preventDefault();
                    }
                }).on('remove', function () {
                    // Remove upload methods from the scope,
                    // when the widget is removed:
                    var method;
                    for (method in uploadMethods) {
                        if (uploadMethods.hasOwnProperty(method)) {
                            delete $scope[method];
                        }
                    }
                });
                // Observe option changes:
                $scope.$watch(
                    $attrs.fileUpload,
                    function (newOptions) {
                        if (newOptions) {
                            $element.fileupload('option', newOptions);
                        }
                    }
                );
            }
        ])

        .directive('fileUpload', function () {
            return {
                controller: 'FileUploadController',
                scope: true
            };

        })
$scope.$on('fileuploadchange',function(e,data){
//here i want to get element which is the source of event
//if you can look at above code piece, i think i can get the element by
//data.scope.mycustomfield
}