Search code examples
javascriptangularjsjqlite

Using jqLite in AngularJS to append text


I'm trying to append html to after the first h1 on the page. angular.element is return the h1 element but it's not appending the text "hello!" after the h1 element.Trying to avoid using full JQuery. Anyone with ideas?

app.controller('Test', ['$scope', function ($scope) {
    var h1 = angular.element(document).find("h1").length > 0 ? angular.element(document).find("h1")[0] : null

    if (h1 != null) {
        angular.element(h1).after(function () {
            return "hello!"
        })
    }
}]);

Solution

  • Do not pass a function in after, just include your desired text as the parameter.

    if (h1 != null) {
            angular.element(h1).after('Hello')
    }