Hey there, I've been struggling to update my model using a directive but for some reason it's not working. I'm having trouble figuring out why. Can anyone lend a hand?
Here is the HTML code snippet:
<input type="file" name="documents" file-model ng-model="documents" multiple>
<button class="btn btn-xs btn-default" ng-click="loadDocuments()">
And here is the directive in question:
app.directive("fileModel", ["$parse",function ($parse) {
return {
restrict: "A",
require:"ngModel",
scope: {
ngModel: '='
},
link: function (scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind("change", function () {
scope.$apply(function () {
scope.ngModel = element[0].files;
});
});
}
};
}]);
This is what I'm getting as output:
$scope.documents={};
$scope.loadDocuments = function () {
console.log($scope.documents); //PRINT UNDEFINED
}