2

I´m trying to create a directive in AngularJS for a small templating issue. In my HTML-File I have this:

<div myinclude="header.tpl.html"></div>

My directive should transform this to that:

<div>{content of file: header.tpl.html}</div>

This is how far (or not) I´ve come, but I guess, I´m missing a piece:

    myApp.directive('myinclude', function () {
    return {
        compile: function compile( tElement, tAttributes ) {
            console.log(tAttributes.myinclude); // logs filename
            return {
                templateURL: tAttributes.myinclude
            };
         }
     };
});

Has anyone done something like this before, and is willing to share?

2

2 Answers 2

1

Here is the working plunker.

Try something like this:

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
    $scope.setValue = function(ele) {
     console.log(ele)
    };
});

app.directive("myInclude",function(){
 return {
     restrict : 'A',
      link : function(scope, element, attrs){
          scope.getContentUrl = attrs["myInclude"];
      },
      template: "content of file : {{getContentUrl}}"
    }
});

HTML

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>

 <div my-include="header.tpl.html"></div>
  </body>

</html>
Sign up to request clarification or add additional context in comments.

11 Comments

Getting closer, but not exactly what I wanted to get: <div myinclude="header.tpl.html" class="ng-scope"> <!-- ngInclude: getContentUrl() --> <div ng-include="getContentUrl()" class="ng-scope">{content of file: header.tpl.html } </div> </div>
i gave what was explained can u please explain a bit more
I would rather have the DIV-Tag just filled with the content like this: <div>{content of file: header.tpl.html}</div>
have you tried this --- .directive('myinclude', function() { return { restrict: 'E', link: function(scope, element, attrs) { scope.tmpName = attrs.myinclude; }, template: '{content of file: {{tmpName}} }' } });
Wouldn´t that just print {content of file: header.tpl.html}? I´d like to have the actual content of that file included between the DIVs. :-)
|
0

Its working...

app.directive('productDescription',function(){
return {
restrict:'E',
templateUrl: 'product-description.html'
};
});

Comments

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.