When attempting to open a link in a new tab, the ng-click function fails to execute

In Angular, utilizing both the <code>ng-click
and ng-href directives at the same time will result in the click function being executed first. In this scenario, clicking on a link that navigates to Google will be prevented and instead an alert will be displayed.

<a ng-href='http://google.com' ng-click="click($event)">Link</a>

$scope.click = function (event) {
    event.preventDefault();
    alert('click has been triggered');
}

It's worth noting that this will only work if the user uses the left mouse button to click the link. If the user attempts to open the link in a new tab, the click event won't be triggered. While this behavior may seem logical, as it isn't technically a "click", is there an Angular directive available for handling the opening of links in new tabs?

UPDATE:

I am not looking to programmatically open a new tab, but rather to handle the event of a user opening a new tab themselves.

Answer №1

An alternative to using ng-href is to implement another function that will trigger the opening of a new tab when clicked.

<a  ng-click="openNewTab(); handleClick($event)">Click Here</a>

$scope.handleClick = function (event) {
    event.preventDefault();
    alert('The link has been clicked');
}
$scope.openNewTab() {
    $window.open('https://www.example.com', '_blank');
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display an Asterisk Icon for md-input fields with lengthy labels

Documentation states that md-inputs add an asterisk to the label if it is a required type. However, when input containers have width constraints and long labels, the label gets truncated and the asterisk becomes invisible. From a user experience perspectiv ...

Solving template strings in a future context

I have a unique use-case scenario where I am filling the innerHTML like this. However, my issue lies in resolving the template literal within the context of a for loop. Any suggestions on how to accomplish this? var blog_entries_dom = 'blog_entries& ...

"Exploring the dynamic duo of Sinatra and AngularJS for seamless

Currently, I am encountering an issue with my Angularjs app running on Sinatra. The problem is that while the Angular routes work when a link within the app is clicked, if I visit the URL directly, I receive a 404 error from Sinatra. I am wondering if the ...

What is the method used for defining an element within an array in JavaScript?

As I am new to JavaScript, I find myself trying to organize callbacks within an array. An example of what I have been working on: items = [ "test" = async message => { let userCoins = editCurrency('fetch', message.guild. ...

Using Angular.JS to iterate over a nested JSON array using ng-repeat

I am currently working on a People service that utilizes $resource. I make a call to this service from a PeopleCtrl using People.query() in order to retrieve a list of users from a json api. The returned data looks like this: [ { "usr_id" : "1" ...

Consolidate radio group in Vuetify platform

I'm having trouble centering the v-radio-group. Here's my current setup: <v-container grid-list-md text-xs-center> <v-form ref="form"> <div v-if="question.question_type == 'YESNO' "> <v-radio-group ...

ngmin failing to properly annotate dependencies across multiple files

Below is the code snippet from my app.js file - var app = angular.module("app", []); In addition, here is what I have in my controller.js file - app.service("Store", function() { this.products = { item: "apple" }; }); app.controller("AppCtrl", function ...

What is the functionality of an Angular service that retrieves an

It appears that Angularjs services are constructed by passing in a Contructor function: app.service('serviceName', function(){ this.var1 = 'foo'; this.method1 = function(){ } }); Angular executes this function using the new ...

Converting PHP array to a JavaScript object

I am facing a scenario like this: Having JSON data: [{ "1377412272": { "user_id": "1374050643", "date": "2013-08-24", "ip": "::1" } }, { "1377412279": { "user_id": "137405064 ...

I'm looking to develop a custom CKEditor plug-in that will allow users to easily drag and drop elements within the editor interface

Upon observing that dragging and dropping text or images within a CKEditor editor instance does not trigger the "change" event, I devised a temporary solution by implementing code triggered by the "dragend" event. However, my ultimate goal is to create a C ...

Developing a unique JavaScript object by extracting information from a jQuery AJAX response

Is there a recommended approach for creating a custom JavaScript object that contains data retrieved from a jQuery AJAX request? I'm considering two methods, but unsure which is the most appropriate. The first method involves including the AJAX reques ...

When utilizing Reactjs, accessing an element by its id becomes challenging when the element is nested within a map() function

Currently, I am encountering an issue related to reactjs. In my scenario, I have a requirement to compare the height of the screen with a specific div to determine its maximum width. The challenge lies in the fact that the particular div I need to analyz ...

Lost item phenomenon: conceal information below until it emerges

I'm attempting to create a garage door effect on my website, where upon loading the page, a garage door is displayed. Upon hovering over the door, it lifts up to reveal the content behind it. The challenge I'm facing is keeping the content hidden ...

What is the best way to identify duplicate keys in fixed JavaScript objects?

My approach so far has been the following: try { var obj = {"name":"n","name":"v"}; console.log(obj); // outputs { name: 'v' } } catch (e) { console.log(e); // no exceptions printed } My goal is to detect duplicate keys within a ...

Unable to set up Angular 1.5 component router

Struggling to incorporate the Angular 1.5 component router into a new project has been quite challenging for me. According to the instructions provided at https://docs.angularjs.org/guide/component-router, running the following command should do the trick: ...

Electron's Express.js server waits for MongoDB to be ready before executing queries

As I work on a demo application, Express serves some React code that interacts with a MongoDB database hosted on mLab. The data is retrieved using SuperAgent calls in my main React code loaded via index.html. While everything works fine when starting the ...

Extract information from JSON using a filter

Is there a way to extract the data value from the list without relying on index positions, considering that the order of arrays can change? I need to specifically target data where code === "size". Unfortunately, the existing structure cannot be ...

What is the best way to generate an array containing multiple arrays, each filled with dynamic Divs?

I have the following code that displays a Div when the user clicks on the Add button. For example, if the user clicks the Add button 5 times, then 5 will be displayed with the same controls/inputs under default. html <div ng-repeat="st in stu"> ...

What is the best way to navigate to a component that has been imported in Vue.js?

I have knowledge of Vue's scrollBehavior function, but I am struggling to integrate it with my existing code. On my Index page, I have sections composed of imported Vue components like this: <template> <div class="Container"> <Ab ...

Having trouble creating an angularjs table using ng-repeat in the controller?

I have a sample snippet that I would like to discuss. My goal is to display a JSON object in an angular table using ng-repeat, which is being generated from my angular controller script. I have attempted the code below, but for some reason, the table is no ...