小编Tom*_*lme的帖子

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

请看这个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".但它没有得到评估.

预期产出 …

javascript angularjs

7
推荐指数
1
解决办法
31
查看次数

标签 统计

angularjs ×1

javascript ×1