Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .load() equivalent AngularJS

Is there an AngularJS equivalent to $.load() in jQuery? I want to be able to pull from another domain (i.e. post the app on one domain, but load content from a completely separate address).

like image 775
wilsonhobbs Avatar asked Sep 28 '22 11:09

wilsonhobbs


1 Answers

jQuery methods of usage don't really fit Angular, you won't find load equivalent there (you obviously can use jQuery instead of jqLite if you want it badly).

Angular proposes the usage of ngInclude directive for similar off-hand scenarios. Otherwise you need to make your own directive and write the result from $http request to element, especially if you need more control.

If you want to 'get content from a particular div', you will need to have jQuery loaded anyway to use selectors on response, something like this would be an equivalent for load:

app.directive('load', function ($http, $compile) {
    return {
        link: function (scope, element, attrs) {
            $http.get('link.htm').success(function (response) {
                var contents = angular.element("<div>").html(response).find("#someelement");
                element.empty().append($compile(contents)(scope));
            });
        }
    }
});
like image 164
Estus Flask Avatar answered Oct 05 '22 06:10

Estus Flask