如何使用angularjs检索html内容

时间:2016-05-03 02:25:41

标签: jquery angularjs

在jquery中,.html()方法有效。

当我在ng-repeat中的角度应用程序中尝试相同时,我什么都没得到。

么?

$(".recentChats").click(function(){
    var value = $(this).html();
    $scope.htmlContent = value;
});

2 个答案:

答案 0 :(得分:0)

无论何时您想使用jQuery从DOM获取或更改某些内容,您都应该使用指令的link函数。

    .directive('clickDirective', function () {
        return function (scope, elem) {
            elem.on('click', function () {
                var value = elem.html();
                scope.htmlContent = value;
            });
        };
    });

HTML

<div ng-repeat="thing in things" click-directive>Do Something {{ thing.name }}</div>

答案 1 :(得分:0)

您可以使用AngularJS $event

Working Demo

HTML:

<div ng-click="clickMe($event)">
    This is test content.
</div>

JavaScript:

$scope.clickMe = function($event){
    console.log($event.currentTarget);
    console.log($event.currentTarget.innerText);
}
相关问题