0

I have developed a transclude directive and I set it to use angularjs template script everything works fine but I still cannot access the bind data.

My code:

index.html

<div side-element="box" title="Links">
                <ul>
                    <li>Link 1</li>
                    <li>Link 2</li>
                </ul>
</div>

<script id="box" type="text/ng-template">
            <div class="side-box">
                <div class="content">
                    <h2 class="header">Box {{ title }}</h2>
                    <span class="content" ng-transclude></span>
                </div>
            </div>
        </script>
        <script id="ad" type="text/ng-template">
            <div class="side-ad">
                <div class="content">
                    <h2 class="header">AD {{ title }}</h2>
                    <span class="content" ng-transclude></span>
                </div>
            </div>
        </script>

app.js:

angular.module('myApp.directives')
  .directive('sideElement', function ($templateCache, $log) {
return {
  scope: {
    title: '@'
  },
  transclude: 'element',
  link: function(scope, element, attrs, ctrl, transclude){
    var template = $templateCache.get(attrs.sideElement);

    var templateElement = angular.element(template);
    $log.info(scope.title);//Output the title i put in the html which is (Links)
    transclude(scope, function(clone){
      element.after(templateElement.append(clone));
    });
  }
};
});

the scope inside the link function(....) displaies the correct title but it doesn't work in the html:

Box {{ title }}

Link 1 Link 2

I think I missed one thing but I can't figure it out, I need your help to complete the cycle.

Thanx in advance,

1 Answer 1

2

The template element that contains the angular binding expressions must be compiled first, and then linked. The compilation phase prepares the template, while the linking phase sets up your $watchers for your binding expressions.

Demo Here

Here is a compile function that should work:

    .directive('sideElement', function ($templateCache, $compile, $log) {

    return {
      restrict: 'A',
      transclude: true,
      scope:'@',
      compile: function(element, attrs) {
        var template = $templateCache.get(attrs.sideElement);
        var templateElement = angular.element(template);
        element.append(templateElement);
        return function(scope, element, attr, ctrl, transclude) {
          $log.info(scope.title);
        }
      }
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Error: [ngTransclude:orphan] Illegal use of ngTransclude directive in the template! No parent directive that requires a transclusion found. Element: <span class="content" ng-transclude="">
change your directive definition to 'transclude: true'
it binds the title, but it keeps generating the same error
I noticed in your template that you have an ng-transclude directive already. Then I don't think you need to call the transclude() function and append the clone manually. Let me know how it goes!
no it didn't work, the same error generated even when removing the transclude() function
|

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.