2013-08-05 68 views
4
<button command="saveCmd">{{saveText}}</button> 

Command指令没有任何模板 - 它是行为指令。但我需要使用transclude: true来显示{{saveText}}如何在Angular指令中使用没有模板的transclude?

我可以创建像template: "<div ng-transclude></div>"这样的虚拟模板,但我不确定div内的按钮是否适用于所有浏览器。

另外我可以使用属性来定义标题,例如<button title="saveText"...但我的问题是关于ng-transclude没有模板。可能吗?

在此先感谢。

更新:

一个新的 '隔离' 范围内scope: {}指令是为什么{{SAVETEXT}}默认情况下不坚持的理由。

+1

默认情况下,如果不使用模板,内容是保持原样。你在做什么指令? [不修改内容的指令示例](http://jsfiddle.net/OverZealous/Ns284/1/) – OverZealous

+0

如果将范围{{}添加到您的JSFIDDLE,{{bar}}将会丢失。 –

回答

1

您可以制作一个没有控制器的指令,也没有隔离范围。在链接功能我有时做这样的事情:

.directive('toggle', function ($parse) { 
    return { 
    /* We can't use an isolated scope in this directive, because it happens 
    * all the time that you need to put a toggle on an element that uses the scope: 
    * <span toggle="boolVar" ng-class="{active: boolVar}">{{someVar}}</span> 
    * 
    * Transclusion can be an option to make the {{someVar}} work, 
    * but the ng-class will use the isolated scope for this directive 
    * (if we'd use the isolated scope, which we don't) 
    */ 
    link: function (scope, $element, attrs) { 
     // use a $parse getter/setter, because toggle can contain a 
     // complicated expression 
     var getter = $parse(attrs.toggle); 
     var setter = getter.assign; 

     $element.on('click', function() { 
     scope.$apply(function() { 
      setter(scope, !getter(scope)); 
     }); 
     }); 
    } 
    }; 
}); 

也许这$解析技巧可以帮助你的命令成立...

相关问题