无法更改数组的数据

unable to change data of an array

本文关键字:数据 数组      更新时间:2023-09-26

这是一些与javascript相关的问题(不是角度,但上下文是有角度的),试图整理几个小时没有任何成功。

我正在将内容修剪为较短的段落,并将子内容分配回数组的内容属性,但显示的仍然是较大的内容。

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();
}

这应该可以帮助您更新内容。

(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);

不要忘记在HTML中使用ng-app="myApp"ng-controller="ControllerName"