Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
    
    
up vote 1 down vote accepted

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() });
    } 
  }; 
});
share|improve this answer
    
In fact, I'm looking for a method other than directive. – user3711105 Dec 14 '16 at 3:10
    
Whats wrong with directive? :) This is basically the angular approach, maybe you just need jquery? – Brian Dec 14 '16 at 3:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.