【发布时间】:2014-02-27 21:05:05
【问题描述】:
我创建了一个生成 Twitter 按钮的指令。由于这些按钮上的范围变量可能会更改,因此我需要在按钮发生时重建按钮。目前,我正在使用 jQuery 来empty() 链接元素并重建按钮。
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()
有没有办法让指令完全重新渲染模板?
【问题讨论】:
标签: javascript jquery angularjs angularjs-directive