First off, new to angularJS so sorry if this question is trivial but my end goal is to change my template whenever a new markdown file is inputed. So right now my directive is looking like this:
app.directive('markdown', function() {
return {
restrict: 'E',
replace: true,
scope: {
fileinput: '='
},
compile: function(elem, attrs) {
attrs.$observe("fileinput", function() {
elem.replaceWith(marked(markdownString));
});
}
}
});
My html is looking like is looking like this
<markDown md="test1.txt"></markDown>
<input type="file" name="filename" id="fileinput" ng-model="input">
Where test1.txt is a markdown file that I convert viat the marked() function into html. Below is how I read in test1.txt.
$("#fileinput").change(function (e) {
if (e.target.files != undefined) {
var reader = new FileReader();
reader.onload = function (e) {
markdownString = e.target.result;
};
reader.readAsText(e.target.files.item(0));
}
});
I can read in a file perfectly, but the the directive will replace the HTML before I have opened and read the file, which is fine, but I want to update the directive and have it run again whenever I input a file. I've tried both attrs.$observe and scope.$watch, but neither seem to be working for me, perhaps I am just writing them incorrectly?
Thanks in advanced!
Edit! Fiddle With the below help I was able to get the input to chanve, but now it's on a delay, not quite what I wanted. Upload files a couple times to see what I mean. Thank you