簡體   English   中英

AngularJS在指令標記之間傳遞html內容

[英]AngularJS pass html content between directive tags

我知道我可以這樣做:

<my-directive attr="myAttr"></my-directive>

然后通過my-directive端訪問attr

但是我想做這樣的事情:

<my-directive attr="myAttr">
 <a href="">Some link that will apply with the my-directive directive too</a>
</my-directive>

這可能嗎?

使用ng-transclude可以解決問題:

// html
<my-directive my-attr="Hello">
 <a href="#">My link</a>
</my-directive>

// my-directive.js 

app.directive("myDirective", function() {
 return {
  transclude: true,
  template: "<h1>{{myAttr}: <ng-transclude></ng-transclude></h1>", // <h1>Hello: <a href...>...</a></h1>
  scope: {
   myAttr: "@"
  },
  link: ($scope, element, attrs) => {

    console.log($scope.myAttr); // Hello

  }
 }
});

在這里閱讀更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM