我试图在新选项卡中打开模式中的所有链接,由于使用角度的限制,操作DOM并不像我想象的那样容易。有没有一种方法可以将click事件绑定到链接,然后使click事件打开一个新选项卡?
我有这样的东西
w = angular.element($window)
w.bind 'click', (e) ->
# if link clicked open link in new tab?
基本规则是,如果您打算对DOM做任何事情,那么您应该有一条指令,并在链接函数中使用普通的JQuery
new-tab.directive.coffee
angular.module 'my.module'
.directive 'newTab', ($window) ->
restrict: 'A'
link: (scope, element, attributes) ->
element.on('click', (evt)->
# Open link in new tab here
)
用法示例:
<a href='/your/link' new-tab>
您可能还必须做一些“防止默认设置”的工作,以使链接仍不会转到当前窗口中的同一页面(现在我已经考虑过了)。
您也可以尝试仅在任何和所有节点上设置_target ='blank'属性。
我还没有测试
new-tab-a.directive.coffee
angular.module 'my.module'
.directive 'a', -> #I doubt this is a good idea though using 'a' as a directive name.
restrict: 'E'
link: (scope, element, attributes) ->
attributes.target = '_blank'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句