<button command="saveCmd">{{saveText}}</button>
Command指令没有任何模板 - 它是行为指令。但我需要使用transclude: true
来显示{{saveText}}
。
我可以创建像template: "<div ng-transclude></div>"
这样的虚拟模板,但我不确定按钮内的div是否对所有浏览器都是有效的html。
此外,我可以使用属性来定义标题,例如<button title="saveText"...
但我的问题是关于ng-transclude没有模板。可能吗?
提前致谢。
更新
一个新的'isolate'范围scope: {}
inside指令是默认情况下{{saveText}}未持久化的原因。
答案 0 :(得分: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));
});
});
}
};
});
也许这个$ parse技巧有助于你的命令设置......