绑定点击事件在新标签中打开link
Bind click event to open a link in new tab
我正在尝试在新选项卡中打开模式中的所有链接,并且由于使用 angular 的限制,操作 DOM 并不像我原以为会的。有没有办法将点击事件绑定到链接,然后让该点击事件打开一个新标签?
我有这样的东西
w = angular.element($window)
w.bind 'click', (e) ->
# if link clicked open link in new tab?
基本规则是,如果你打算用 DOM 做任何事情,那么你应该有一个指令并在 link 函数
中使用正常的 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>
您可能还需要做一些 "prevent default" 的事情,这样 link 就不会在当前 window 中仍然转到同一页面(现在我认为关于它)。
您也可以尝试在任何和所有节点上设置 _target='blank' 属性。
I haven't tested this
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'
你不能对每个 link 使用 ng-click 吗?
<a href="#" ng-click="openLink('http://www.google.com')">Open link in new window</a>
并且在你的控制器中有这样的东西:
$scope.openLink = (link) ->
window.open link
return
或仅使用 html:
<a href="http://www.google.com" target="_blank">Open link in new window</a>
我正在尝试在新选项卡中打开模式中的所有链接,并且由于使用 angular 的限制,操作 DOM 并不像我原以为会的。有没有办法将点击事件绑定到链接,然后让该点击事件打开一个新标签?
我有这样的东西
w = angular.element($window)
w.bind 'click', (e) ->
# if link clicked open link in new tab?
基本规则是,如果你打算用 DOM 做任何事情,那么你应该有一个指令并在 link 函数
中使用正常的 JQuerynew-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>
您可能还需要做一些 "prevent default" 的事情,这样 link 就不会在当前 window 中仍然转到同一页面(现在我认为关于它)。
您也可以尝试在任何和所有节点上设置 _target='blank' 属性。
I haven't tested this
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'
你不能对每个 link 使用 ng-click 吗?
<a href="#" ng-click="openLink('http://www.google.com')">Open link in new window</a>
并且在你的控制器中有这样的东西:
$scope.openLink = (link) ->
window.open link
return
或仅使用 html:
<a href="http://www.google.com" target="_blank">Open link in new window</a>