I'm just starting to learn Angular and have been getting to grips well with the basics of it. However I've come unstuck, as my wider knowledge of JS is also pretty limited.
I'm creating a feed that brings in data from a JSON file. One of the items I wish to bring in is a string with HTML inside:
"description": " <p><a href=\"https://www.flickr.com/people/71256895@N00/\">tomylees<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/71256895@N00/20384585533/\" title=\"American Sweet Potatos IMG_6026\"><img src=\"https://farm1.staticflickr.com/608/20384585533_4141b76f92_m.jpg\" width=\"240\" height=\"180\" alt=\"American Sweet Potatos IMG_6026\" /><\/a><\/p> <p>Going to the farm shop.<br /> Rose Farm Shop, Rose Farm Cottages, Haine Rd, Ramsgate CT12 5AG.<br /> Saturday, 29th August 2015, Ramsgate, Kent.<\/p>",
I'm already parsing the HTML into the view using ngSanitize and ng-bind-html without issue.
Where I am coming unstuck, is that every string of HTML starts with two <p> elements that I don't want to display.
Basically I want create a filter that does this:
var trim = require('trim');
var $ = require('jquery');
angular.module('FlickrFeed')
.filter('removePara', function () {
return function(input) {
var $html = $('<div></div>').append($.parseHTML(input));
$html.find('p:nth-child(1), p:nth-child(2)').remove();
var output = trim($html.text());
return output || 'No description available.';
};
});
But, I don't want to use jQuery, as it feels overkill for this tiny part. I'm also aware that my filter won't need to parse my HTML, as ngSanitize takes care of this before it hits the filter (is this why I'm struggling?).
Main question: how do I isolate and remove the first two <p> elements? Obviously the conditional 'No description available' would be an added bonus, but I'll settle for just getting rid of those two paragraphs.
Cheers,
Jamie.