[英]How to catch event when click on link with the same url in AngularJS?
I have 2 links: 我有2个链接:
<a href="#link1">First</a>
<a href="#link2">Second</a>
And 2 controllers: 和2个控制器:
app.controller('firstCtrl', function($scope) {
$scope.$on('$locationChangeStart', function (event, nextPage, currentPage) {
alert("ok");
}
app.controller('secondCtrl', function() {}
When I go from second link to first link, I see alert. 当我从第二个链接转到第一个链接时,我看到警报。 How how to add event, if I stay on 1st link (=firstCtrl) and click on 1st link again?
如果我停留在第一个链接(= firstCtrl)并再次单击第一个链接,如何添加事件?
Attention: I want to get new and old url 注意:我要获取新旧网址
If you just want to trigger the alert each time when the first link is clicked, you may need to use ng-click
attribute rather than $on('$locationChangeStart')
event listener, which will fire only when url path changes: 如果您只是想在每次单击第一个链接时触发警报,则可能需要使用
ng-click
属性而不是$on('$locationChangeStart')
事件侦听器,该事件侦听器仅在URL路径更改时才会触发:
HTML HTML
<div ng-controller="firstCtrl">
<a ng-click="link1()">First</a>
</div>
<div ng-controller="secondCtrl">
<a ng-click="link2()">Second</a>
</div>
JS JS
app.controller('firstCtrl', function($scope) {
$scope.link1 = function() {
alert("ok");
}
});
app.controller('secondCtrl', function() {...});
Actually, for this scenario, it is better to change the links to buttons, so that users will expect these elements to hold some local operations instead of new pages(partials). 实际上,对于这种情况,最好更改按钮的链接,以便用户期望这些元素保留一些本地操作,而不是新页面(部分)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.