0

I copied this somewhere:

angular.module('app')
  .directive('ffInclude', ['$http', '$templateCache', '$compile' ,function($http, $templateCache, $compile) {
    return function(scope, element, attrs) {
      var templatePath = attrs.ffInclude;
      $http.get(templatePath, { cache: $templateCache }).success(function(response) {
        var contents = element.html(response).contents();
        $compile(contents)(scope);
      });
    };
  }]);

The point of this directive is work like ng-include but without creating a new scope.

It can be used like this:

<div ff-include="my-template.html"></div>

My question is as follows: I would like to use this also for a general template, and in this template I would like to switch out something (an attribute of a html-element).

So if I have this html:

<div ff-include="my-general-template.html" new-attribute-value="false"></div>

And the template could look like this:

<div ng-show="true">Here is some general content</div>

In the directive I should get the attribute like this:

var newAttributeValue = attrs.newAttributeValue;

But how to swap this with new attribute with whatever value is in the ng-show attribute in the template?

Edit: I made a plunk...

7
  • plnkr.co/edit/UxVbTq0Ik7VcTEzWNTxB?p=preview Commented Sep 10, 2014 at 12:46
  • set it to true above, i set it to false. Commented Sep 10, 2014 at 12:47
  • Oh, @NoypiGilas, completely forgot one thing, this creates a new scope, and the entire reason for the directive was to keep the original scope. Is there a way to do this without creating a new scope? Commented Sep 10, 2014 at 13:56
  • was this the question deleted yesterday? Commented Sep 11, 2014 at 4:35
  • @NoypiGilas, I was doing many things too quickly yesterday... Commented Sep 11, 2014 at 6:56

1 Answer 1

1

I like how this idea of doing general templating... =)

The solution below will not isolate the scope. http://plnkr.co/edit/jFj8MHZ9Kalrk4qO35IX?p=preview

  return function(scope, element, attrs) {
    scope.newAttributeValue = attrs.newAttributeValue;
    console.log("new=", scope.newAttributeValue);

at the general template:

 <div ng-show="newAttributeValue">
Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect, @NoypiGilas, it was really simple! Thanks a lot :)
glad to have help. =)

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.