Passing variable to directive template without creating new scope

后端 未结 2 1220
陌清茗
陌清茗 2021-02-12 03:43

Is there a way to pass variables using attributes to a directive without creating a new scope ?

HTML

&l
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-12 04:04

    I guess you should use compile function in this case.

    angular.module('myApp').directive("button", function () {
        return {
            template: "
    {{button}}
    ", replace: true, scope: true, compile: function (tElement, tAttrs) { // this is link function return function (scope) { scope.button = tAttrs.button; }; } }; });

    Here is jsfiddle example.

提交回复
热议问题