我有这样的代码:
<my-button>{{text}}</my-button> (The 'text' is not so sure, can be the other like: 'mytext')
myApp.directive("myButton", function(){
function linkFunc(scope, element, attrs){
//How to get the {{}} variable name and value?
//not always '{{text}}' can be {{text1}} {{text2}}
}
return {
link: linkFunc,
restrict: "E",
template: "<button></button>,
transclude: true,
replace: true
}
});
我知道我可以使用 element.text()
获取文本并在指令中重新编译它。还有其他方法吗?
最佳答案
您需要像以前一样将您的指令标记为“transclude”,但您还需要一个内容插入点。在这种情况下,它就在按钮内部,因此您可以这样做:
app.directive('myButton', function () {
return {
restrict: 'E',
template: '<button ng-transclude></button>',
transclude: true
}
});
关于javascript - 如何在指令中获取 {{text}}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058280/