यदि आप कुछ अधिक सुरुचिपूर्ण / एकीकृत चाहते हैं, तो आप समर्थन के साथ निर्देश का विस्तार करने के लिए एक डेकोरेटर का उपयोग कर सकते हैं । ध्यान में रखने के लिए मुख्य चेतावनी यह है कि यह विधि IE9 में काम नहीं करेगी क्योंकि IE9 ने फ़ाइल एपीआई को लागू नहीं किया था । XHR के माध्यम से प्रकार की परवाह किए बिना द्विआधारी डेटा अपलोड करने के लिए जावास्क्रिप्ट का उपयोग करना IE9 या पूर्व में मूल रूप से संभव नहीं है ( स्थानीय फाइल सिस्टम तक पहुंचने के लिए उपयोग नहीं करता है क्योंकि ActiveX का उपयोग केवल सुरक्षा समस्याओं के लिए पूछ रहा है)।input
type=file
ActiveXObject
इस सटीक विधि के लिए भी AngularJS 1.4.x या बाद के संस्करण की आवश्यकता होती है, लेकिन आप इसके $provide.decorator
बजाय इसका उपयोग करने के लिए इसे अनुकूलित करने में सक्षम हो सकते हैं angular.Module.decorator
- मैंने यह दिखाने के लिए जॉन पापा की AngularJS शैली मार्गदर्शिका के अनुरूप यह प्रदर्शित करने का तरीका लिखा है :
(function() {
'use strict';
/**
* @ngdoc input
* @name input[file]
*
* @description
* Adds very basic support for ngModel to `input[type=file]` fields.
*
* Requires AngularJS 1.4.x or later. Does not support Internet Explorer 9 - the browser's
* implementation of `HTMLInputElement` must have a `files` property for file inputs.
*
* @param {string} ngModel
* Assignable AngularJS expression to data-bind to. The data-bound object will be an instance
* of {@link https://developer.mozilla.org/en-US/docs/Web/API/FileList `FileList`}.
* @param {string=} name Property name of the form under which the control is published.
* @param {string=} ngChange
* AngularJS expression to be executed when input changes due to user interaction with the
* input element.
*/
angular
.module('yourModuleNameHere')
.decorator('inputDirective', myInputFileDecorator);
myInputFileDecorator.$inject = ['$delegate', '$browser', '$sniffer', '$filter', '$parse'];
function myInputFileDecorator($delegate, $browser, $sniffer, $filter, $parse) {
var inputDirective = $delegate[0],
preLink = inputDirective.link.pre;
inputDirective.link.pre = function (scope, element, attr, ctrl) {
if (ctrl[0]) {
if (angular.lowercase(attr.type) === 'file') {
fileInputType(
scope, element, attr, ctrl[0], $sniffer, $browser, $filter, $parse);
} else {
preLink.apply(this, arguments);
}
}
};
return $delegate;
}
function fileInputType(scope, element, attr, ctrl, $sniffer, $browser, $filter, $parse) {
element.on('change', function (ev) {
if (angular.isDefined(element[0].files)) {
ctrl.$setViewValue(element[0].files, ev && ev.type);
}
})
ctrl.$isEmpty = function (value) {
return !value || value.length === 0;
};
}
})();
यह पहली जगह में क्यों नहीं किया गया? AngularJS समर्थन IE9 के रूप में केवल वापस तक पहुँचने का इरादा है। यदि आप इस निर्णय से असहमत हैं और सोचते हैं कि उन्हें इसे वैसे भी लागू करना चाहिए था, तो वैगन को एंगुलर 2+ पर कूदें क्योंकि बेहतर आधुनिक समर्थन का शाब्दिक अर्थ है कि कोणीय 2 मौजूद है।
मुद्दा यह है (जैसा कि पहले उल्लेख किया गया था) कि फाइल एपीआई समर्थन के बिना यह ठीक से कर रहा है कोर के लिए हमारे बेसलाइन IE9 दिया जा रहा है और इस सामान को पोलीफ़िलिंग कोर के लिए सवाल से बाहर है।
इसके अतिरिक्त इस इनपुट को ऐसे तरीके से संभालने की कोशिश की जाती है जो क्रॉस-ब्राउज़र संगत न हो, केवल 3 पार्टी समाधान के लिए कठिन बना देता है, जिसे अब कोर समाधान से लड़ना / अक्षम करना / कार्य करना है।
...
मैं इसे बंद करने जा रहा हूँ जैसे हमने # 1236 को बंद किया। आधुनिक ब्राउज़रों का समर्थन करने के लिए कोणीय 2 का निर्माण किया जा रहा है और इसके साथ फ़ाइल समर्थन आसानी से उपलब्ध होगा।