在AngularJS中使用jqLit​​e来附加文本

时间:2016-06-08 10:20:11

标签: javascript angularjs jqlite

我正在尝试将html附加到页面上的第一个h1之后。 angular.element返回h1元素,但它没有附加文本“hello!”在h1元素之后。尽量避免使用完整的JQuery。谁有想法?

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!"
        })
    }
}]);

2 个答案:

答案 0 :(得分:1)

不要在after中传递函数,只需包含所需的文本作为参数。

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

答案 1 :(得分:1)

angular.element(h1)更改为$(h1)

    $(h1).after(function () {
        return "hello!"
    })

http://jsfiddle.net/ms403Ly8/94/