Updates to AngularJs models are not being reflected

I am facing an issue with my model that I want to make editable, but for some reason nothing changes - the textbox fails to appear and the model remains unchanged when using ng-view.

I can confirm that the function enableEditor() is being triggered as I see it in the console log.

If I include the code inline in the index.html without the profile.html, everything works perfectly fine.

Below are the relevant files:

app.js

var proGamersApp = angular.module('proGamersApp', ['ngResource']).
    config(function ($routeProvider) {

        $routeProvider.
        when('/', { controller: 'ProfileController', templateUrl: '/app/partials/profile.html' }).
        otherwise({ redirectTo: '/' });
    });

var ProfileController = function ($scope) {

    $scope.init = function () {
        $scope.title = 'first title';
    };

    $scope.init();
    $scope.enableEditor = function () {
        console.log('enableEditor()')
        $scope.editorEnabled = true;
        $scope.editableTitle = 'second title';
        $scope.title = 'second title';
    };

     ...
};

index.html

<!doctype html>
<html ng-app="proGamersApp">
<head>
    <title>Pro Gamers</title>

    <!-- Scripts -->
    <script src="/app/lib/angular.min.js"></script>
    <script src="/app/lib/angular-resource.js"></script>
    <script src="/app/app.js"></script>

</head>
<body>
    <div ng-view></div>
</body>
</html>

profile.html

 <div ng-hide="editorEnabled">
        {{title}}
      <a href="#" ng-click="enableEditor()">Edit title</a>
    </div>
    <div ng-show="editorEnabled">
        <input ng-model="title" ng-show="editorEnabled">
        <a href="#" ng-click="save()">Save</a>
        or
      <a href="#" ng-click="disableEditor()">cancel</a>.
    </div>

Can anyone help me identify what I might be doing wrong?

Thank you

Answer №1

The URL is being added to your address, which causes the router to refresh the page and reset all of your $scope variables. Instead of using empty anchor tags, try using a span styled to look like an anchor:

span:hover {
    cursor:pointer;
}

This will change the cursor to a pointer when hovering over the span. Feel free to customize the colors as you see fit. Also, based on your comments, don't include target=_self in the href attribute; instead, add it after the tag:

<a href="#" target=_self ng-click="save()">Save</a> //prevents changing the address bar

However, I recommend sticking with spans instead.

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

Top technique for extracting json files from post requests using nodejs

Situation: I'm running a Node.js REST server that receives JSON files, parses them, and inserts them into a database. With an anticipated influx of hundreds of requests per second. Need: The requirement is to only perform insertions by parsing the JS ...

I'm sorry, but we were unable to locate the module: Error: Unable to find 'fs' in '/usr/src/app/node_modules/jpeg-exif/lib'

Encountering an error when building a react app on production: Module not found: Error: Can't resolve 'fs' in '/usr/src/app/node_modules/jpeg-exif/lib' Node Version: Node version 18 Steps taken in Docker Production: npm install - ...

What could possibly prevent Jasmine Spyon from being named?

I am currently facing an issue with a failing test even though I have included the necessary calls. One specific area where I am encountering this problem is with the PrimeNG Message Service that I am spying on. Below, you can find the code snippet that I ...

Utilize Vue to localize the click events of buttons on the webpage

I have a scenario where I have several buttons on a page, and when I click one to toggle its text, all of the buttons change. How can I utilize VUE to isolate functionality and make each button's @click event only affect the clicked button? In the cod ...

Mastering the use of Action.Submit in adaptive cards to simulate user input

I am trying to implement MessageFactory.SuggestedActions within my "welcomeCard" adaptive card. Essentially, in my adaptive card (welcome card), I have several buttons for the user to click on, each with an Action.Submit type. { "type" ...

Having trouble updating the input value in AngularJS?

As I venture into customizing an AngularJS tutorial on a Saturn Quiz, I am transforming it from multiple choice to a fill-in-the-blank quiz. The challenge I face is that the first answer registers as correct or incorrect, but subsequent questions always s ...

The route is displaying the variable as 'undefined' when I attempt to access it

I had set up CRUD for two different models (venues & artists) - venues works fine, but when I try to access 'artists/index', it gives me an error saying 'Artists is not defined'. After examining the code, I believe I need to do two ...

Can two Angular element components be utilized simultaneously on a single page in Angular 6?

Currently, I'm attempting to host independent Angular elements that can be seamlessly integrated into a non-Angular webpage. Everything works perfectly fine when there's only one element on the page, but as soon as I try to load two or more, I en ...

The `Target closed` error in Playwright Library is triggered solely by the closing of either the `context` or `browser`

The code snippet below showcases a Node.js script that leverages the Playwright Library for browser automation (not Playwright Test) to extract data from a local website and display the results in the terminal. The challenge arises when the script encounte ...

Generating distinctive content within the confines of the Selenium WebDriver

Is there a way to generate a unique username value for the signup page username textbox using selenium webdriver instead of hardcoding it? For example: driver.findElement(By.id("username")).sendKeys("Pinklin") ; When "Pinklin" is hardcoded, running the ...

The dynamic addition of the active class is malfunctioning

I have dynamically added a class using AngularJS, but it seems to not be functioning correctly. As I am new to AngularJS, I would appreciate any suggestions to resolve this issue. Below is my HTML code: <div class="col-md-15 col-sm-3" ng-repeat="data i ...

While the NPM package functions properly when used locally, it does not seem to work when installed from the

I have recently developed a command-line node application and uploaded it to npm. When I execute the package locally using: npm run build which essentially runs rm -rf lib && tsc -b and then npm link npx my-package arguments everything works sm ...

Tips for automating the activation of intents at specific scheduled times in Dialogflow

I'm attempting to automatically trigger intents in Dialogflow to obtain the user's contact details at a scheduled time. Is it possible to achieve this using JavaScript? If so, could you please provide the code? ...

Obtain the node identifier within the Angular UI Tree component [angular-ui-tree]

Utilizing Angular UI tree to create a relationship between items and categories has been successful in the default setup. However, a new requirement has emerged to incorporate node/section numbering within the tree for managing hierarchy. I attempted to i ...

Utilizing AngularJS and Bootstrap popover: A guide to effectively implementing "popover-is-open" within an ng-repeat loop

When utilizing ng-repeat to generate a table with items, each item has a button in its row that opens a popover for editing the object. I want to avoid using the click-outside property of the bootstrap popover because I only want the popover to close when ...

I'm interested in knowing if it's feasible to develop unique jQuery selectors that can navigate ancestors, such as a :closest or :parents selector

As a developer who frequently creates jQuery plugins, I often find myself using custom jQuery selectors like :focusable and :closeto to simplify common filters in my code. For example, the :focusable selector is defined as follows: jQuery.extend(jQuery.e ...

How do I return a <div> element to its initial state after JavaScript has made changes to it?

So, I have this situation where a DIV contains a form. After users submit the form successfully, I want to replace the form with a simple message saying "everything is good now". This is how I currently do it: $("#some_div").html("Yeah all good mate!"); ...

What is the appropriate way to incorporate a dash into an object key when working with JavaScript?

Every time I attempt to utilize a code snippet like the one below: jQuery.post("http://mywebsite.com/", { array-key: "hello" }); An error message pops up saying: Uncaught SyntaxError: Unexpected token - I have experimented with adding quotation m ...

Experience real-time updates on webpages with Node.js and Socket.io

Seeking to integrate real-time push notification updates from a node.js server to the browser client. Explored socket.io at http://socket.io/docs/rooms-and-namespaces/ The business requirement involves users viewing a page with customer information and o ...

Angular Material Toolbar Experiencing Color Distortion

To see the issue in action, check out this CodePen: An ongoing problem I've encountered with Angular Material involves distorted colors on the toolbar. The edges of the toolbar display one shade of green, while the middle shows a different shade. Tak ...