1

I am having following directive:

app.directive('renderPartial', function($compile) {
   return {
      restrict: "AE",
      link: function(scope, element, attrs) {
         var path = getPartial(attrs.module, attrs.file);
         //path = /abc/some_file.html
         scope[attrs.model] = path;
         var el = $compile('<div ng-include="attrs.model"></div>')(scope);
         element.html(el);
      }
   }
});

In my view:

<render-partial module="abc" file="some_file" model="some_model"></render-partial>

Now for some reason this is not working, no errors. But file is not getting rendered.

Plunkr for my problem: http://plnkr.co/edit/CkTE2pV4i5LvL60NEYfE

1
  • ping check my updated answer :-P Commented Mar 25, 2015 at 10:42

2 Answers 2

1

UPDATE

According to plunker attached in the comment:-

You need to do couple of things

1) Use var el = $compile('<div ng-include="'+attrs.model+'"></div>')(scope);

2) element.append(el); instead of plane HTML :-P

Plunker

Sign up to request clarification or add additional context in comments.

4 Comments

No, I want this to work: var el = $compile('<div ng-include="scope[attrs.model]"></div>')(scope);
I way saying put only value of attrs.model inside your ng-include don't include scop becuase it is a glue b/w view and logic.So, suppose attr.model has 'one' then scope[attrs.model] will turn to scope[one] which is equivaluent to scope.one and in view it will be only 'one'. You don't need scope word in front of it.
If I try this var el = $compile('<div ng-include="attrs.model"></div>')(scope); still its not working
@ntechi can you replicate it in fiddle or plunker ?
0

You can make it dynamic, like this:

myApp.directive('renderPartial', function($compile) {
   return {
      restrict: "AE",
      link: function(scope, element, attrs) {
         var path = 'test.html';
         scope.path = path;
         var el = $compile('<div ng-include="path"></div>')(scope);  
         element.append(el);
      }
   }
});

Fiddle

1 Comment

This will not work if I have multiple same directive calls with different files, cause the scope is not array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.