javascript - 如何在指令中获取 {{text}}?

标签 javascript angularjs directive

我有这样的代码:

<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
    }
});

Fiddle

关于javascript - 如何在指令中获取 {{text}}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058280/

相关文章:

javascript - 没有 ng-animate 的 Angular 动画,可能吗?

angularjs - 嵌套指令和 ng-repeat 异常 TypeError : Cannot read property 'insertBefore' of null

assembly - 6502 : Where it is stored 中的.byte指令

javascript - 如何通过变量引用父对象?

CSS 父选择器取决于存在的另一个类

javascript - 如何在JS中保存PDF文件(来自ajax)

angularjs - 从现有的 angularjs 代码生成 uml 类图

javascript - 将 $watch 附加到指令中的输入字段

javascript - .eq 返回所有选择

javascript 将对象数组的数组求和在一起