AngularJS - Use a directive to hide a div upon clicking inside an inner element

I am attempting to create a function that will hide a specific box when clicking on a link within that same box.

My goal is to utilize a directive in order to easily add additional functionality once the box is hidden, making it adaptable for other views as well.

I initially tried using an isolated scope but it seems I may have implemented it incorrectly.

For reference, here is a JSFiddle link: http://jsfiddle.net/rJvqf/

In my controller, I have:

$scope.hideLoginBox = false;

Within my view, there is a div with ng-hide="hideLoginBox"

<div ng-app="MyApp">
  <div ng-controller="MyController">
    <div class="base" ng-hide="hideLoginBox">
        <p>outer</p>
            <div class="child">
                <a href="" my-test="" hidebox="hideLoginBox" >
                    innner
                </a>
            </div>
    </div>
  </div>
</div>

My intention is to use the myTest directive to hide this div when the link is clicked.

I am currently unsure of how to achieve this and would greatly appreciate any assistance provided!

Thank you for your help!

Warm regards, Eric

Answer №1

So close! All you needed was a scope.$apply because you were in the middle of a bind event within an element, and I switched it to bind to click instead of mousedown.

Check out this working jsfiddle link.

Updated Code:

  element.on('click', function(event) {
    scope.hideLoginBox = true;
    scope.hidebox = true;
    scope.$apply();
    console.log(scope);   
  });

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

Adjusting the Height of a Div Using a Single Button

Seeking help to make a div expand and then shrink with one button click. I've tried various methods but the div won't change size. Can someone guide me on why this is happening? Here is the code, CSS, and HTML for my latest attempt: $("dasButto ...

What is the best way to continuously execute the 'onclick' function using AJAX inside a specific ID?

My challenge involves implementing a new AJAX upload feature to insert a data element from the onClick event in multiple rows. The issue I am facing is that it works fine for the first row, but when I add subsequent rows, the function no longer works upon ...

Incorporating React into an already existing webpage and accessing URL parameters within a .js file

Following the official guidelines: To include React in a website, visit https://reactjs.org/docs/add-react-to-a-website.html I have created a file named test.html with the following content: <!DOCTYPE html> <html> <head> < ...

Defer the rendering of Vue.js pages until the data request is completed

I am currently working on a page that retrieves data from the server using axios. My goal is to wait for the axios request to complete before rendering the page content. The reason behind this approach is that I already have a prerendered version of the ...

ng-src attribute not updating in ng-repeat directive

Just starting with angularjs and I'm attempting to grab an img id from ng-repeat, pass it through a function, and return an image string. However, I keep encountering errors. Any assistance would be appreciated. HTML <tr data-ng-repeat="(key, lis ...

Display the year-month-day format as YYYY-MM-DD rather than the full date

Struggling to update a date linked by ngModel using ngx-bootstrap datepicker and send it in a PUT request to my Django backend. The date format keeps changing from YYYY-MM-DD (2019-08-13) to the full JavaScript date string (2019-08-13T23:00:00.000Z), causi ...

There is a SyntaxError due to an unexpected token "o" in the JSON data. It is not a valid JSON format

This code is designed to check an XML file to see if the email or login inputted by a user is already in use, and then provide a JSON response. The AJAX code for this functionality is as follows: $.ajax({ type: 'post', dat ...

How can you detect a click event on a hidden DIV without capturing clicks on any elements within the DIV, especially when the DIV is not present in the Document Object Model (DOM)

Currently, I am facing an issue with my vanilla JavaScript event listener. It seems to be passing data correctly, except when I click on text that is formatted as bold or italicized. Below is the JavaScript code that I am testing: window.onclick = functio ...

Updating object values within a while loop using JavaScript

I am facing an issue with managing an array of JavaScript objects that have a property 'table' with values table1, table2, table3, or table4. Each table should only accommodate 6 members. I have implemented a while loop to check if 'table1&a ...

Swapping one div for another, all the while incorporating a limited number of buttons within the webpage

I'm faced with a dilemma on my HTML page. In the middle of the content, there's a div that contains 4 links with images and text inside. What I'm looking to achieve is for each link to trigger a complete change in the div when clicked, with ...

"Is it possible to trigger a JavaScript function within an HTML file from a servlet in Java in order to update the

Is there a way to invoke an HTML JavaScript function from a servlet in Java that will update the content of the HTML page? Could you please provide me with a sample example? I have one server-side servlet that writes data which needs to be updated in ano ...

Troubleshooting Vue: Why is my component not visible when using v-form and v-text-field components in Vuetify?

Attempting to implement the v-form and v-text-field components from the Vuetify node package. <template> <v-form> <v-text-field label="Test" type="foo"></v-text-field> <v-text-field label="bar&q ...

Attempting to save MongoDB data into a variable for integration with Handlebars.js

Having an issue with storing MongoDB data in a variable to display in HTML using hbs. The specific error message is TypeError: Cannot read property 'collection' of undefined. Here's the code snippet I have written: const express = require(& ...

What is the best way to transform the request query id = ' [12eetftt76237,jhgasduyas7657] ' into an array of elements or strings like [12eetftt76237,jhgasduyas7657]?

Hey there, I am working on a project using hapijs and typescript. I have a requirement to send an array of IDs as parameters through the request URL. Here is an example of the URL: localhost:3444/?id='[askjajk78686,ajshd67868]' I attempted to u ...

What is the best way to retrieve the total number of nested objects within an object?

I'm trying to figure out how to get the number of nested objects in the object a. a = { firstNested: { name: 'George' } secondNested: { name: 'James' } } I thought about using .length, which is typ ...

Update data in the datatables using values from an array list

Currently, I have two HTML files where I am loading data using JSON and then applying jQuery datatables to them. Now, I need to refresh the data with new parameters. For example: JSON: [ {"name":"jon","sales":"100","set":"SET1"}, {"name":"charlie","sale ...

Using Jquery Regex to Validate Numeric Range Input on KeyUp Event in an Input Field

I have been spending quite a few hours attempting to figure out a solution for this issue, but unfortunately, I haven't had much success. My goal is to add a Keyup/KeyPress event that only allows values between 2 and 1827. I am using an aspx inputbox ...

"Encountered a TypeError: Cannot read property 'params

I've encountered an issue with passing the id to my product page. Despite trying various solutions and searching for answers, I still can't get it to work. Below is my index.js code: import React from "react"; import {render} from &quo ...

Tips for maximizing efficiency and minimizing the use of switch conditions in JavaScript when multiple function calls are needed

After coming up with this code a few minutes ago, I began to ponder if there was a more elegant way to optimize it and condense it into fewer lines. It would be even better if we could find a solution that eliminates the need for the switch condition altog ...

Tips for showcasing a full body height background

I'm currently working on a project where I need to display random background images when the body loads. To achieve this, I've created a function. However, I'm facing an issue where the images are filling up the entire page, making it very l ...