0

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?

2

1 Answer 1

1

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() });
    } 
  }; 
});
Sign up to request clarification or add additional context in comments.

2 Comments

In fact, I'm looking for a method other than directive.
Whats wrong with directive? :) This is basically the angular approach, maybe you just need jquery?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.