AngularJS append HTML in the ng-repeat element

后端 未结 1 544
情书的邮戳
情书的邮戳 2020-12-12 02:59

Using AngularJS I need to append HTML to the elements of ng-repeat, using the selector: \'objectMapParent-\' + post._id

  
相关标签:
1条回答
  • 2020-12-12 03:36

    How to Encapsulate jQuery code in a Custom Directive

    Any jQuery code that manipulates the DOM should be encapsulated in a custom directive so that it is executed when the AngularJS framework ng-repeat directive instantiates the element.

    <div ng-repeat="post in posts">
        <div id="{{ 'objectMapParent-' + post._id }}">
             <h5 style="color:blue; margin-top: 3px;">{{post.title}}</h5>
             <my-element post="post"></my-element>
        </div>
    </div>
    
    app.directive("myElement", function() {
        return {
            link: postLink,
        };
        function postLink(scope,elem,attrs) {
            var post = scope.$eval(attrs.post);
            var html = `
              <div id="objectMap-${post._id}">
                <div id="BasemapToggle-${post._id}">
                </div>
              </div>
            `;
            elem.append(html);
        }
    })
    

    To use jQuery, simply ensure it is loaded before the angular.js file.

    For more information, see

    • AngularJS Developer Guide - Creating Custom Directives
    • AngularJS angular.element API Reference
    0 讨论(0)
提交回复
热议问题