Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http HTML Parser

All, we're developing a webapp with AngularJS and we have a use case/requirement (that won't happen very often at all) where we will need to retrieve a complete HTML document from our static server. However, it appears that the $http object returns a raw HTML string as its 'data'. We are trying to avoid using an external library (like jQuery, for instance), but we need to parse that raw HTML string into a queriable DOM object. We could use an iframe and be done with it, but we're also trying to avoid using iframes for well-known reasons.

So, the question is: does AngularJS have a parser for HTML (as it does for JSON)? Or else, what's the most graceful way to handle this case?

P.S.: We tried sifting through Angular's API docs, but in all honesty they are hit-or-miss and unintuitive to navigate around.

like image 256
rdodev Avatar asked Oct 30 '12 14:10

rdodev


People also ask

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.

How do you modify the $HTTP request default Behaviour?

To add or overwrite these defaults, simply add or remove a property from these configuration objects. To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults.

How do we pass data and get data using HTTP in angular?

Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.

What is $location in AngularJS?

The $location in AngularJS basically uses a window. location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS.


2 Answers

If you need to get a DOM element from the string programmatically, parsing html into a DOM object is pretty simple.

Basically you'd use DOMParser.

var parser = new DOMParser();
var doc = parser.parseFromString('<div>some html</div>', 'text/html');
doc.firstChild; //this is what you're after.

From there if you wanted to query to get elements from it with vanilla JS (meaning, you don't want to use JQuery for some reason), you can do this:

//get all anchor tags with class "test" under a div.
var testAnchors = doc.firstChild.querySelectorAll('div a.test'); 

... but note: querySelectorAll is only supported in IE8 and higher.

EDIT: additional approach...

The "wrap" method:

var div = document.createElement('div');
div.innerHTML = '<div>some html</div>';
var result = div.childNodes;

... do note that this method will return HTMLUnknownElements if you put SVG or MathML into it. They'll "look" normal, but they won't work.

like image 187
Ben Lesh Avatar answered Sep 30 '22 10:09

Ben Lesh


First of all, Angular uses jQuery as well. (jQuery lite)

From FAQ

Does Angular use the jQuery library?

Yes, Angular can use jQuery if it's present in your app when the application is being bootstrapped. If jQuery is not present in your script path, Angular falls back to its own implementation of the subset of jQuery that we call jQLite.

However, I think you don't need full jQuery function here anyway.

That is what I did.

I use a directive and get the html as template and set replace as true.

jsFiddle

<myhtml></myhtml>

angular.module('myApp', []).directive('myhtml', function($http) {


    return {
        restrict: 'E',
        scope: {},
        template:'',
        link: function(scope, element, attrs) {
           $http.post('/echo/json/', data).success(function(re) {
               element.html(re.html);
        });
    }
    }
});

Edit note:

I just update the jsFiddle.

I include jquery on top for echoing the request in jsFiddle. In real, you don't need that.

However, it shows you that you can use jQuery in Angular.

If your html doesn't contain any angular tag, this example should work as your expectation .

Otherwise, you need to use compile instead of link.

like image 29
maxisam Avatar answered Sep 30 '22 10:09

maxisam