Issue with ng-show not toggling visibility on ng-click event in AngularJS

When I click on an Edit Mode link, I want to display some Edit icons. I've tried using ng-click with ng-class and ng-show but it's not working as expected.

Here is the HTML code:

<div click-to-edit="questions.n1.name" class="row question"></div>
    <a href="" ng-click="editMode = !editMode">Enter edit mode</a>

And this is the click-to-edit directive:

.directive("clickToEdit", function () {
var editorTemplate = '' +
'<div class="click-to-edit">' +
    '<div ng-hide="view.editorEnabled">' +
        '{{value}} ' +
        '<a class="glyphicon glyphicon-pencil" ng-show="editMode" ng-click="enableEditor()"></a>' +
    '</div>' +
    '<div ng-show="view.editorEnabled">' +
        '<input type="text" class="" ng-model="view.editableValue">' +
        '<a class="glyphicon glyphicon-ok" href="#" ng-click="save()" ></a>' +
        '<a class="glyphicon glyphicon-remove" ng-click="disableEditor()"></a>' +
    '</div>' +
'</div>';

return {
restrict: "A",
replace: true,
template: editorTemplate,
scope: {
    value: "=clickToEdit",
},
link: function (scope, element, attrs) {
    scope.view = {
        editableValue: scope.value,
        editorEnabled: false
    };

    scope.enableEditor = function () {
        scope.view.editorEnabled = true;
        scope.view.editableValue = scope.value;
        setTimeout(function () {
            element.find('input')[0].focus();
        });
    };

    scope.disableEditor = function () {
        scope.view.editorEnabled = false;
    };

    scope.save = function () {
        scope.value = scope.view.editableValue;
        scope.disableEditor();
    };

    }
};
})

I've included a Plunker link for reference. In script.js, line 11, the ng-show is supposed to be triggered by the ng-click on the html (line 20).

What could be the issue here? Do I need to make any changes within the clicktoEdit directive in order to trigger the ng-show?

Answer №1

If your directive has an isolated scope, you may encounter issues accessing certain variables like editMode. One way to solve this problem is by explicitly referencing the parent scope property:

<a class="glyphicon glyphicon-pencil" ng-show="$parent.editMode" ng-click="enableEditor()"></a>

You can see a demo of this solution here: http://plnkr.co/edit/8M98LGOfdRrBp5IaaGKZ?p=preview

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

Unexpected error occurred when attempting to fetch the jQuery value of the radio button: "Unrecognized expression syntax error"

I am facing an issue while trying to extract the value of a radio button using $("input[@name=login]"); I keep getting an "Uncaught Syntax error, unrecognized expression" message. To view the problem, visit http://jsfiddle.net/fwnUm/. Below is the complet ...

Customize the appearance of the Vue.js datepicker starting from today's date

I am currently using the vue-datepicker component to display a date input field in my form. I want to set the default date to the current day and disable the selection of past dates. Additionally, I need to change the language of the component but it seems ...

Tips for setting up Craigslist email forwarding through Node.js (receiving emails to a dynamically generated email address and redirecting them)

After extensive searching, I only came across this Stack Overflow post about Email Forwarding like Craigslist in Rails: Email Forwarding like Craigslist - Rails I spent a significant amount of time googling, but couldn't find any solutions using Node ...

What is the process for importing specific modules from jQuery?

When working with webpack or browserify, what is the specific procedure required to import only the essential modules from jQuery as mentioned here? import {core, dimensions} from 'jquery' Unfortunately, this approach does not seem to be effect ...

Issue with cross-browser compatibility: jQuery fails to display the first item in the select dropdown

Here is a select box found within a search form: <select name="searchFor" id="searchFor"> <option name="option0" value="Choice">Pick one</option> <option name="option1" value="person">Person</option> <option name="optio ...

Exploring Vue.js: Accessing nested elements

Trying to convert this to vuejs has presented some challenges. First off, How do I access nested sub elements? <div ref="tickerwrapper" class="tickerwrapper"> <ul ref="list" class='list'> <li re ...

Anticipate that the function will remain inactive when the screen width is less than 480 pixels

Implementing Functionality in Responsive Navigation <script> document.addEventListener('DOMContentLoaded', () => { let windowWidth = window.Width; let withinMobile = 480; let close = document.getElementById('close&ap ...

Combining Angular variables within HTML tags: A Guide to AngularJS

As a newcomer to AngularJS, I am truly impressed by its capabilities. One thing I am eager to learn is how to include an AngularJS binding within an HTML attribute, while also adding another string to it. I often use unique names like "thisform" and "thisd ...

The Structure of Ionic Controllers and Services

As someone who is new to Ionic and AngularJS, I recently attempted to create a note-taking app. However, I encountered an issue where my controllers.js file did not recognize the services.js file. Can anyone provide guidance on how to resolve this issue? T ...

Testing the Mongoose save() method by mocking it in an integration test

I am currently facing an issue while trying to create a test scenario. The problem arises with the endpoint I have for a REST-API: Post represents a Mongoose model. router.post('/addPost', (req, res) => { const post = new Post(req.body); ...

Is it possible to pass arguments to setTimeout function?

At the current moment, my code looks like this: function showGrowl(lastNumber) { var num = lastNumber; //keep generating a random number untill it is not the same as the lastNumber used while((num = Math.ceil(Math.random() * 3)) == lastNumber); ...

Adding a plethora of HTML using jQuery

Struggling to create a single-page website, I've found myself relying heavily on jQuery's .append function. But surely, there has to be a more efficient method for appending large chunks of code (like tables, graphs, etc.) onto a page. Take this ...

Is it possible to assign a width property to a div element?

I want DivTest and IdOtherDIV to have the same width. I attempted to set properties like this: DivTest { background: #007C52; width: document.getElementById("IdOtherDIV").scrollWidth + "px.\n"; } Is there a way to achieve this (considering tha ...

Tips for incorporating nonce or sha into the connect-src directive in Content Security Policy (CSP)

Can a nonce be used in an API request to make sure that the connect-src in CSP doesn't flag it as a malicious address? It appears that nonce can only be used in script-src or style-src, not in connect-src. So far, I have only been able to list URLs ...

Tips for sending a JavaScript variable value to jQuery validate() submitHandler while performing an AJAX request

My validation on ajax calls is currently using a jQuery plugin. This is the current setup: $("#invoiceForm").validate({ rules: { plateNumber: { required: true, }, plateIssueState: { required: true, } }, ...

UPDATE: An issue has been identified with the /login route that is currently causing an unknown error

Hours of coding and suddenly my app is rendering everything twice. Can't figure out why, please help! https://i.sstatic.net/RhMid.png app.js import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import "bootstrap/ ...

Guide on fetching user information (such as picture and name) from Google Cloud Endpoints using AngularJs upon page initialization

My goal is to automatically send a post request using Google Cloud Endpoints and AngularJS upon page load in order to retrieve user information such as profile picture and description. While I can successfully make requests by clicking a button, I am faci ...

What is the process for dynamically incorporating JavaScript files during compilation to serve as input for browserify?

Within our project's structure, there exists a directory housing multiple js files. The possibility of adding or removing these files later on is present. Currently, we have a file named main.js where each file is imported and a map is created (using ...

Utilize the three.js library within a Vue component by importing it and incorporating its

Can someone please help me understand the proper way to import and utilize the three.js library in a vue component? Despite my extensive research, it seems that most individuals use the following code line to import three.js into a vue component. However, ...

Make the switch from using jQuery MouseEvent to utilizing AngularJS for a more

Presently, in my Angular directive within the link function, I am using the following code: (This directive is placed at a top level in the dom.) $(document).on("mousedown", ".resizer", function (e) { e.preventDefault(); resizing = true; frame ...