Jacky Lau
Jacky Lau

Reputation: 733

AngularJS get html element

I'm using angularjs, and I want to obtain the html element by simply mark an ng-tag.

<div ng-xxxx="myDiv"></div>

console.log($scope.myDiv); //<div ng-xxxx="myDiv"></div>

Can someone tech me how it can be done?

Upvotes: 0

Views: 1758

Answers (1)

Brian
Brian

Reputation: 338

Here is a plunkr sample that should be close to what you're looking for, or at least a good starting point.

https://plnkr.co/edit/QVd5WJEGq7B9WWyXJBSu?p=preview

So here is an attribute directive, that uses method binding (& binding), which means it accepts an angular expression getHtml: '&' So here the parent is using the expression to set a scope variable to $content get-html="$ctrl.string = $content". $content is defined by the directive itself in a map, when the expression is executed in the link function. scope.getHtml({ $content: elem.html() });

.directive('getHtml', function() {
  return {
    scope: {
        getHtml: '&'
    }, 
    restrict: 'A',
    link: function(scope, elem, attr) {
        scope.getHtml({ $content: elem.html() });
    } 
  }; 
});

Upvotes: 1

Related Questions