Freewind
Freewind

Reputation: 198318

Is it possible to set some value to the element in `postLink` function in `compile` of `directive` in angularjs?

The doc of angularjs:

myModule.directive('directiveName', function factory(injectables) {
  var directiveDefinitionObject = {
    priority: 0,
    template: '<div></div>',
    templateUrl: 'directive.html',
    replace: false,
    transclude: false,
    restrict: 'A',
    scope: false,
    compile: function compile(tElement, tAttrs, transclude) {
      return {
        pre: function preLink(scope, iElement, iAttrs, controller) { ... },
        post: function postLink(scope, iElement, iAttrs, controller) {
                         // ????
                         // is it possible to set the content of iElement?
                    }
      }
    }
  };
  return directiveDefinitionObject;
});

In the postLink function inside compile, is it possible to set some text to the iElement?

I've tried:

iElement.html("some");
iElement.textContent = "some";
jQuery(iElement).html("some");

But it seems none of them worked.

Upvotes: 0

Views: 201

Answers (1)

bmleite
bmleite

Reputation: 26880

iElement.html("some"); should work as expected: http://jsfiddle.net/bmleite/vjnN7/

app.directive('someDirective', function() {
  return {
    priority: 0,
    template: '<div>Test...</div>',    
    replace: false,
    transclude: false,
    restrict: 'A',
    scope: false,
    compile: function compile(tElement, tAttrs, transclude) {
      return {
        pre: function preLink(scope, iElement, iAttrs, controller) {  },
        post: function postLink(scope, iElement, iAttrs, controller) {
                iElement.html('Some other test...');                         
             }
      }
    }
  };  
});

Upvotes: 1

Related Questions