Modifying the $scope property on ng-click and redirecting with ui-sref

I am completely baffled by this situation.

Here is the current status:

https://i.sstatic.net/PJdb6.png

Upon reaching this page, the following appears in the console:

https://i.sstatic.net/aIJqd.png

When I click on "lorem2", which has an ng-click function meant to change the displayed object on the large square on the left. Strangely, it logs this in the console:

https://i.sstatic.net/3Njp1.png

It does indeed change the value of the variable, but then reverts back to the original object.

Oddly enough, if I click again, the following occurs:

https://i.sstatic.net/psCdp.png

Now, the change remains and "lorem2" is displayed in the big square. Any thoughts on what might be causing this behavior?

This code block is within the controller:

$scope.singleArticle = $scope.articlesList[0];
console.log($scope.singleArticle);


$scope.changeArticle = function(article){
    $scope.singleArticle = article;
    console.log($scope.singleArticle);
};

This is the function that is triggered on ng-click to update the value.

Below is some relevant HTML code:

<div class="col-lg-7">
    <div class="panel panel-default">
        <div class="panel-body">
            <p>{{singleArticle.title}}</p>
            {{singleArticle.content}}
        </div>
    </div>
<div class="col-lg-3">
    <div class="panel panel-default" ng-repeat="article in articlesList | limitTo: (1 - articlesList.length) | limitTo: 3">
        <div class="panel-body">
            <a ui-sref="articles_route({ article: article.title})"
               ng-click="changeArticle(article)">{{article.title}}
            </a> {{article.content | limitTo: 100}}.
        </div>
    </div>
</div>

Thank you for your attention :)

Answer №1

The issue lies in passing the article as a parameter to the changeArticle function. The article is actually the key of the articlesList returned by ng-repeat.

To resolve this, you should send the value within the articlesList array that each iteration of ng-repeat is presenting.

Utilize track by $index to track ng-repeat by index and pass the index of each array item to the function using articlesList[$index]

    <div class="col-lg-7">
    <div class="panel panel-default">
        <div class="panel-body">
            <p>{{singleArticle.title}}</p>
            {{singleArticle.content}}
        </div>
    </div>
<div class="col-lg-3">
    <div class="panel panel-default" ng-repeat="article in articlesList | limitTo: (1 - articlesList.length) track by $index">
        <div class="panel-body">
            <a ng-click="changeArticle(articlesList[$index])">{{article.title}}
            </a> {{article.content | limitTo: 100}}.
        </div>
    </div>
</div>

You can find more information about ng-repeat syntax here.

Furthermore, assuming you want to update the URL path as mentioned in your comments on the original post, the provided code accomplishes this change in content as requested.

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

Javascript function that sets the opacity to 0

Hello everyone, I'm new to SO and seeking some guidance on improving my post! I have a function that sets the opacity of an element to 0 in order to clear it. While this works fine, it becomes burdensome when dealing with multiple elements that requi ...

"Troubleshooting issue: Unable to retrieve grid data from Google Sheets API v4 when integrated

I've implemented a function within a React component that retrieves JSON data from an API request and logs it: async function getAllSheets() { let response; try { response = await window.gapi.client.sheets.spreadsheets.values.batchGet( ...

Using Three.js, I implemented a feature that allows the mousewheel to adjust the camera's position vertically rather

I am trying to achieve a specific effect in my Three.js project. I created a scene using the Three.js Editor and downloaded the project with the "Publish" option. By editing the app.js file to import OrbitControls, I have enabled zoom functionality with th ...

Loading controller.js files dynamically as needed versus combining and compressing all controllers into a single file and linking it

Being new to angularjs development, I am uncertain about the best approach for my situation. As I work on a large-scale application, the number of controllers keeps increasing day by day. I am looking for advice on which approach would be better in terms o ...

When you use Array.push, it creates a copy that duplicates all nested elements,

Situation Currently, I am developing a web application using Typescript/Angular2 RC1. In my project, I have two classes - Class1 and Class2. Class1 is an Angular2 service with a variable myVar = [obj1, obj2, obj3]. On the other hand, Class2 is an Angular2 ...

Ways to make a $resource in Angular return example data?

Let's imagine I have an angular service with the following resource: var res = $resource('/myurl/:index', {index: '@index'}) Is there a way for me to customize paths so that when I use: $res.query() I receive a predefined outpu ...

Troubles encountered during the installation of Visual Web Developer 2010?

Here is the situation I'm facing: After encountering some issues with PHP while working on an ASP.net project, I decided to reinstall everything. The installation of Apache, MySQL, and PHP went smoothly, but now I seem to be facing some problems. I ...

Unveiling the Power of Ionic and React for Component Repetition

I'm having trouble figuring out how to repeat my component multiple times using react in Ionic. Can someone assist me with this? Here's an example: In my Component.tsx file, I have the following code: import React from 'react'; import ...

Is it possible to send an array value from JavaScript Ajax to PHP and then successfully retrieve and read it?

As a newcomer to PHP, I am facing a challenge in saving multiple values that increase dynamically. I'm passing an array value using the following method from my JavaScript code. $(".dyimei").each(function(i,value){ if($(this).val()=="") ...

Concerning geoext

I am currently working on creating a framework for overlaying layers on the web. I am using geoserver to publish the layers and the geoext tree.js example to display all the layers in a tree-like panel. However, I'm encountering an issue with zooming ...

Error: When using @ViewChild, it is not possible to access the property 'nativeElement' if it is undefined

Currently, I am working on a project using Angular-7. I have been trying to access elements directly from the TypeScript file by utilizing @ViewChild, but encountered an error message. The code snippet in question is: @ViewChild('serverContentInput&ap ...

Separate every variable with a comma, excluding the final one, using jQuery

I've developed a script that automatically inserts checked checkboxes and radio options into the value() of an input field. var burgerName = meat + " with " + bread + " and " + onion + tomato + cheese + salad; $('#burger-name').val(burger ...

The HTML5 range input is consistently defaulting to the minimum value and is not triggering the onChange event for text input

My goal is to implement a feature where an input type text is used along with an input type range with three specific use cases: 1. Default case: When the page loads, the slider should point to the value specified in the text input field. 2. When a user e ...

Is it necessary to have a strong understanding of JavaScript in order to effectively utilize jQuery?

While I currently do not have knowledge of JavaScript, I am intending to acquire it soon. My query is whether a strong grasp of JavaScript is necessary to utilize jQuery effectively. I am already familiar with ActionScript and PHP, which share certain si ...

Enhance translation functionality within the loader module of ngx-translate

I have been tasked with building a unique Angular ngx-translate Loader that retrieves translations from either a cache or an API. I am currently facing a challenge in Case 2 section of the code: Here is the desired process: Reterieve translations from c ...

Achieving cell merging in react-table can be accomplished by utilizing the appropriate

Currently, I am using react-table and have the need to combine cells in specific columns based on their content. Essentially, I want to remove the divider border between these merged cells. To give you a visual idea, this is how it currently looks like: h ...

Surprise mistake: Jade's error with the length

What can I do to address this problem? The jade file in question is as follows: extends layout block content h1. User List ul each user, i in userlist li a(href="mailto:#{user.email}")= user.username U ...

Import a Blender Animation into Three.js with the Three.js JSON exporter

Currently, I am taking my first steps in JavaScript and exploring Three.js as well. One of the things I am learning is how to export and load a Blender animation into Three.js. To help me with this process, I am using an example from the Three.js website: ...

What could be causing JQuery to disrupt my HTML code by inserting additional <a> tags?

My dilemma involves this specific chunk of HTML code stored within a javascript string, while utilizing Jquery 1.6.1 from the Google CDN: Upon executing console.log(string): <a><div class='product-autocomplete-result'> & ...

Unlocking the full potential of AngularJS comparator

I created this jsfiddle to experiment with the AngularJS comparator functionality, but I am encountering an issue: Check out my jsFiddle Comparator here It appears that my custom comparator function 'wiggleSort' is not being called as expected: ...