我创建了一个生成Twitter按钮的指令。由于这些按钮上的范围变量可能会更改,所以我需要在发生按钮时重新生成按钮。目前,我使用jQuery来链接元素empty()
并重建按钮。如何在Angular指令中重新呈现模板?
app.directive 'twitterShare', ($timeout, $window) ->
restrict: 'E'
template: '<a href="https://twitter.com/share" class="twitter-share-button" data-text="{{ text }}" data-url="{{ url }}">Twitter</a>'
scope:
text: '@'
url: '@'
link: (scope, el, attrs) ->
scope.$watch 'text', -> rebuild()
scope.$watch 'url' , -> rebuild()
rebuild = ->
$(".twitter-share-button").remove()
tweet = $ '<a>'
.attr 'href', 'https://twitter.com/share'
.attr 'id', 'tweet'
.attr 'class', 'twitter-share-button'
.attr 'data-lang', 'en'
.attr 'data-count', 'none'
.text 'Tweet'
el.prepend tweet
tweet.attr 'data-text', scope.text
tweet.attr 'data-url', scope.url
$window.twttr.widgets.load()
有什么办法让指令完全重新渲染模板吗?
按照OP的例子,在使用模板的情况下应如何使用transclude功能?当我尝试为这个例子添加一个模板时,它似乎没有被使用 - 我猜是因为克隆。 – StephenT
伟大的东西伙伴,我真的需要这样的东西。我已经添加了一个$编译选项,以便我可以重新编译一个组件。我将在底部发布这些更改。 – Mattijs