0

I'm trying to do something similar to the following: Plunker

I know that i can use $filter service but I think that inline filtering is more expressive.

If anyone can take a look at link above and tell me where is my mistake.

Edit(using $interpolate): Plunker

I'm not sure If I'v used $interpolate the right way but as you can check here I think I did.

Community
  • 1
  • 1
Aviel Fedida
  • 4,004
  • 9
  • 54
  • 88

1 Answers1

1

my guess is that Angular has no knowledge of the template, you need to manually compile it or preferably, use the template key when creating the directive.

I modified your snippet here, but basically it boils down to:

mainApp.directive('mainContainer', function($parse){
  return {
    template: '<h1>{{ lowerString | CapFirst }}</h1>',
    link: function(scope, elem) {
      scope.lowerString = 'lol';
    }
  };
});

Edit

As requested, here is an alternative snippet that uses the $compile service:

mainApp.directive('mainContainer', function($compile){
  return function(scope, elem) {
    var lowerString = 'lol';
    var html = $compile('<h1>{{ "' + lowerString + '" | CapFirst }}</h1>')(scope);
    elem.append(html);
  };
});

This could also work if you used $interpolate instead of $compile. The benefit of using $compile is that if you were using a template with bindings to the scope, $compile would properly bind the scope to the template, whereas $interpolate would not.

yggie
  • 297
  • 3
  • 8