1

I am generating dynamic HTML and prepending it this way:

$scope.prepareContent = function(content, id) {
    $scope.myContent = 
        '<div class="foo" id="' + id + '">' +
            '<a href="#" ng-click="delete(' + id + ')">Delete this content</a>' +
            content +
        '</div>';
}

divOne = angular.element( document.querySelector('.one') );
divOne.prepend($scope.myContent);

1 - I would like to know how can I do this using a directive.

2 - I know that I have to use $compile in order to use the link generated this way. How to do this?

2
  • What do you try to achieve ? I smell a usecase for a native angular directive here (Probably ng-repeat ?). The custom directive may not be the best way to solve this. Add theses informations to have a better help. Commented May 28, 2015 at 16:04
  • This system is a soccer match narrative where the admin can post content and this content will be inserted in particular DIVs like "regular-time", "penalt", "after-match". To insert the content I am using TinyMCE and I got the correct result, the problem is when I want to insert the link tag and I would like to better organize the code. Commented May 28, 2015 at 16:06

3 Answers 3

2

Here's a simple example of how you can construct your directive.

app = angular.module('myApp', []);

app.directive("myDirective", function(){
        return {
          restrict: 'AE',
          replace: 'true',
          template: '<h3>Hello World!!</h3>', //put your html content here..
          scope: {
              param1: "=",            
              param2: "=",            
              param3: "="            
          },
          link: function(scope){
            //how to access passed params..
            console.log(scope.param1);
            console.log(scope.param2);
            console.log(scope.param3);
          }
      };
    })

On your html then you can call this directive as..

<my-directive param1="paramVal1" param2="paramVal2" param3="paramVal3"></my-directive>
Sign up to request clarification or add additional context in comments.

2 Comments

bawun, same problem commented bellow.
Sergio, I think you can set the contents of each section ("regular-time", "penalt", "after-match") to an json object and just use ng-repeat on the contents.. This way you can make a template static but the contents dynamic <div ng-repeat="content in contents"> <div id="content.id"><a href="delete(content.id)">Delete this content</a></div> </div> </div>
1

Try something like this.

angular.module('myApp')
  .directive('deleteLink', [function ($timeout, $sce) {
      return {
        restrict: 'E',
        scope: {
          id: '='
        }
        replace: true, // Replace with the template below
        link: function (scope, element, attrs) {
          scope.delete = function () {
            //Write delete logic here using scope.id
          };
        },
        template: '<div class="foo"><a href="#" ng-click="delete()">Delete this content</a></div>'
      };
    }
  ]);

Usage

<delete-link id="pass the id here"></delete-link>

1 Comment

OK ShankarSangoli, the problem is that I can't use <delete-link id="pass the id here"></delete-link> because this content will be inserted in different DIVs according to some system logic. Thats why I use append to do it. Is there any way to do it without the html tag?
1

Consider using a native ng-repeat for your needs.

HTML

<div>
  Regular time
  <div ng-repeat="content in regularTime.contents">
     {{content}}
     <a href="#" ng-click="regularTime.splice($index,1)">Delete this content</a>
  </div>
</div>

Controller

$scope.regularTime = [];
$scope.regularTime.push("Fantastic !");

Use one different tab per section you need.

Hope it meets your needs or at least gave you some clues.

1 Comment

Thanks Okazaki and Bawun, I will try this approach and see what happens.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.