无法更改数组的数据

时间:2015-01-07 22:28:40

标签: javascript angularjs

这是一些与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();
}

1 个答案:

答案 0 :(得分:0)

这可以帮助您更新内容。

(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"