incapable of modifying the contents of an array

Struggling with a JavaScript issue (not angular related, but within an Angular context) for hours without any success.

I've condensed the content into shorter paragraphs and reassigned the subcontent back to the array's content property, but the larger content still appears.

controller: function($scope, $location) {
    this.$scope = $scope;

    this.$scope.folio = {
        "deleted"       : null,
        "free"          : null,
        "id"            : null,
        "lastModified"  : null,
        "productId"     : null,
        "publication"   : null,

        articles: []
    };
},

result: function(folio) {
    this.$scope.folio.articles.length = 0;

    this.$scope.folio.deleted = folio.deleted
    this.$scope.folio.free = folio.free
    this.$scope.folio.id = folio.id
    this.$scope.folio.lastModified = folio.lastModified
    this.$scope.folio.productId = folio.productId
    this.$scope.folio.publication = folio.publication

    for(var i=0; i<folio.articles.length; i++) {

        for(var j=0; j<folio.articles[i].pages.length; j++) {
            var content = folio.articles[i].pages[j].content
            var index = content.toLowerCase().indexOf("test".toLowerCase())

            if(index > 150) { //if index is not within first 150 characters
                content = content.substring(index - 25, index + 30)
            }
            //displays fine with the desired output
            console.log(content.replace(new RegExp("test", "gi"), "<em>" + "test" + "</em>"))

            //assigning back to the array but for some reason it's not the trimmed output at the end
            folio.articles[i].pages[j].content = content.replace(new RegExp("test", "gi"), "<em>" + "test" + "</em>")
        }

        //the article's pages are still full length
        this.$scope.folio.articles.push(folio.articles[i]); 
    }

    this.$scope.$apply();
}

Answer №1

If you need to update your content, this code snippet might be helpful.

(function(angular){
    var myApp = angular.module('myApp');

    myApp.controller('Controller', function(){
        $scope.folio = {
            "deleted"       : null,
            "free"          : null,
            "id"            : null,
            "lastModified"  : null,
            "productId"     : null,
            "publication"   : null,

            articles: []
        };

        $scope.result = function(folio) {
            $scope.folio = folio;

            angular.forEach($scope.folio.articles, function(article, articleIndex)) {
                angular.forEach(article.pages, function(page, pageIndex)) {
                    var content = page.content,
                        index = content.toLowerCase().indexOf("test".toLowerCase());

                    if(index > 150) { //if index is not within first 150 characters
                        content = content.substring(index - 25, index + 30)
                    }
                    //displays fine with the desired output
                    console.log(content.replace(new RegExp("test", "gi"), "<em>" + "test" + "</em>"))

                    //assigning back to the array but for some reason it's not the trimmed output at the end
                    $scope.folio.articles[i].pages[j].content = content.replace(new RegExp("test", "gi"), "<em>" + "test" + "</em>")
                }

                $scope.folio.articles.push(folio.articles[i]); 
            }

            $scope.$apply();
        }
    });

})(window.angular);

For proper functionality in your HTML file, remember to include ng-app="myApp" and ng-controller="ControllerName".

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

Displaying Stats.js inside a different canvas using ThreeJS

Just starting out with Three.js and wanted to test displaying the Stats.js in a small scenario. Check it out here Decided not to use modules for now, but followed similar code structure as in the examples: var stats = new Stats(); var renderer = ...

Acquiring the Facebook profile_id and incorporating it into a URL using a Chrome Extension

Although I have limited knowledge of JavaScript, I believe I know enough to complete the task at hand; I just need some guidance. I am working on developing an extension that will extract the profile_id from the current Facebook page being viewed, and the ...

Is it safe to use $http.get() in AngularJS to request a JSON configuration file securely?

Using a relative path, I am retrieving a JSON file that contains sensitive authentication data with the following code: $http.get('app/config.json').then(function (data) {} Although it's currently functioning correctly, I have concerns abou ...

Can dynamic CSS imports be unloaded in a React application?

I am facing an issue with loading two files using react.lazy and suspense: import React, { Suspense, lazy } from "react"; import { Route, Redirect } from 'react-router-dom'; const MainLayout = lazy(() => import('Components/Layout/MainL ...

Guidelines on how to retrieve information from an AJAX request in PHP

I am in the process of developing an application that involves calling a JS function in PHP like this: $data = "<script>JSFunction();</script>"; The JSFunction is defined as follows: function JSFunction(){ $.ajax({ type: 'POST' ...

Using Web SQL always seems to throw me for a loop

I attempted to populate a web SQL database with some data, but encountered an issue. Here is my code: database(); for(var i=0; i<m.length; i++){ showid = m[i].id; showtitle = m[i].title; insert(); } function database(){ //open the database ...

Using AngularJS to inject a service into a static property of a standard class

For my current project, I am combining TypeScript and AngularJS. One of the challenges I'm facing is how to instantiate a static member of a public class (not controller, just a normal class) with a service object. When it comes to controllers, utiliz ...

Data retrieval seems to be encountering issues in Firefox and IE9, whereas Chrome and Safari are functioning without any problems

I am using the following method function callCommentservice() { try { // Comment Service Url var getCommentServiceUrl = self.commentsServiceUrl + self.getRating + "tenantId=" + self.tenantId + "&ratedObjectTypeId=" + sel ...

"Exploring the functionality of HTML buttons on iOS Safari with Angular click

Currently, I am developing a web app that includes a feature where users can hold down a button to adjust a value. The backend of the app is supported by Meteor.js with Angular serving as the front end. The functionality works perfectly, except for Mobile ...

The modal disappears when the user clicks on the Previous/Next buttons of the jQuery UI datepicker

Using the jQuery datepicker from https://jqueryui.com/datepicker/ along with the UIkit framework found at I'm trying to incorporate the datepicker within a form that is inside a modal window. The issue arises when the modal window disappears after i ...

Tips for interpreting a JSON object that has been returned

I'm currently working on creating a notification system. The process involves running an AJAX call that executes a PHP script to select all 'message' items where 'new=1'. The PHP array is then returned to the JavaScript function vi ...

I'm having trouble with the change and onchange functions not working on my computer. Does anyone know what could be causing this issue

I'm having an issue with the code below. It should trigger an alert box when I select a file or make a change to the textbox and then click out of it. Strangely, when I run the code snippets separately on JSFiddle, everything works perfectly. So I&ap ...

Using JavaScript to Transfer Data Between Web Pages

For our landing page, we're planning to incorporate two levels of questions for the respondents. Initially, the respondents will be asked to choose up to four interest tracks using checkboxes. Once they have made their selections, they can proceed to ...

Displaying an array value instead of a new value list in a React component

Situation - Initial number input in text field - 1 List of Items - 1 6 11 Upon removing 1 from the text field, the list becomes - New List Items - NaN NaN NaN Now, if you input 4 in the field. The updated List Items are - NaN NaN 4 9 14 Expected ...

Accessing an object from multiple React components

I am working with a MapboxMap React component that initializes and displays a Mapbox map (stored in the map const variable), and requires rendering of MapMarker components within it. This is the structure of the MapboxMap component: import React from &ap ...

What is the best way to display my 'React Loading Spinner' component at the center of my view?

Whenever I use the Loading as my parent component for the Layout component, it always shows up in the center of the page instead of within my view. How can I adjust its positioning? ...

Daily loop countdown timer

Looking to implement a daily countdown timer that ends at 10am, I have the following code set up: setInterval(function time(){ var d = new Date(); var hours = 09 - d.getHours(); var min = 60 - d.getMinutes(); if((min + '').length == 1){ ...

Steps for adding buttons in a popover

I've been attempting to embed HTML attribute buttons into a popover in Bootstrap 5 using JavaScript. My goal is to create a clear button with the following function: However, the button is not displaying, only the text within the popover. https://i ...

Encountering ERR_EMPTY_RESPONSE when using the $.POST method

I have been struggling with a issue on my social networking site that includes chat functionality. I have utilized the $.post method extensively across multiple pages, and it generally functions well except for a specific page called message.php where user ...

Tips for successfully passing an object with a list property in an ajax request

I have encountered a similar problem to This previous question, but I am struggling to implement the solutions provided. I am unsure of where to include the ModelBinder code mentioned in the responses, so I will explain my issue here. I am working with a s ...