First you have to do event.preventDefault() to complete the time for $http to complete the process. To do this in Angular, you must pass $event the ng-click handler.
HTML example:
<body ng-controller="appController"> <a href="#" ng-click="navigate($event)">LINK</a> </body>
applications:
var app = angular.module('app', []); app.controller('appController', function ($scope, $http, $window, $log) { // generic function for window navigation function nav(url, newTab) { if (newTab) { $window.open(url, '_blank'); } else { $window.location.href = url; } } // handler for ng-click directive $scope.navigate = function (event) { // This is the key -> preventing default navigation event.preventDefault(); // determine whether to open a new page or not var newTab = ((event.which === 1 && (event.metaKey || event.ctrlKey)) || event.which === 2); // get the URL from a file $http.get('links.json') .success(function (data, status, headers, config) { // navigate when URL is fetched nav(data.someLink, newTab); }) .error(function (data, status, headers, config) { $log.debug('Error:', data, status); }); }; });
See an example of an open $event object here .
source share