Directive的模板没有获得由compile设置的值

Tom*_*lme 7 javascript angularjs

请看这个Plunker

我有一个使用自定义角度指令的HTML

  <body ng-controller="myCtrl">
    <h1>Hello Plunker!</h1>
    <div><sample attributeone="Sample Attribute"></sample></div>
  </body>
Run Code Online (Sandbox Code Playgroud)

我的指令看起来像这样:

myApp.directive('sample', function() {
    var value = "";
        return {
            replace: true,
            restrict: 'E',
            scope : false,

            template: '<div>This is a sample Paragraph '+ value + '</div>',

            compile: function ( tElement, tAttributes ) {
              return {
                  pre: function preLink( scope, element, attributes ) {
                      console.log( attributes.log + ' (pre-link)'  );
                      value = tAttributes.attributeone;
                  }
              };
         }
        };
});
Run Code Online (Sandbox Code Playgroud)

在我看来,对precompile应该执行bofore模板返回,并value应设置为"Sample Attribute".但它没有得到评估.

预期产出

This is a sample Paragraph Sample Attribute
Run Code Online (Sandbox Code Playgroud)

实际产出

This is a sample Paragraph
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以设置value模板?

Ram*_*asi 2

如果您只想添加value,那么为什么不将模板用作函数呢?

查看这个更新的Plunker

myApp.directive('sample', function() {
    var value = "";
        return {
            replace: true,
            restrict: 'E',
            scope : false,

            template: function(ele, attr, ctrl) {
              value = attr.attributeone;
              return '<div>This is a sample Paragraph ' + value + '</div>';
            }
        };
});
Run Code Online (Sandbox Code Playgroud)