【问题标题】:How to append the given value in array in angular js如何在角度js中的数组中附加给定值
【发布时间】:2015-06-17 04:44:38
【问题描述】:

JSFiddle

这是我的保存功能,我需要在本地存储中保存为数组。下面的代码显示了尝试过的内容??我该如何解决这个问题?在我做错的地方,所有数组中都附加了相同的值

有人可以帮忙解决这个问题吗?

$scope.savealbum = function() {

            var AlbumTitle = $("#AlbumTitle").val(); // 345
            var AlbumDescription = $("#AlbumDescription").val(); // 345
            var ImageName = $("#ImageName").val(); // 345
            var youtube1 = $scope.youtubelink1; // 345
            var youtube2 = $scope.youtubelink2; // 345
            var videocomment1 = $scope.videocomment1; // 345
            var videocomment2 = $scope.videocomment2; // 345
            var imagearray = $scope.imagearray; // 345


             var Album = {
                 album_desc: AlbumDescription,
                 id: 1,
                 album_title: AlbumTitle,                
                 ImageName: ImageName,
                 youtube1: youtube1,
                 youtube2: youtube2,
                 videocomment1: videocomment1,
                 videocomment2: videocomment2,
                 album_pics: []
             };



             // if (JSON.parse(localStorage.getItem('album')!="")) {
             var newPics = {
                 "media_type": imagearray['_file']['type'],
                 "last_modified_date": imagearray['_file']['lastModified'],
                 "thumnail_pic_loc": imagearray['_file']['name'],
                 "large_pic_loc": imagearray['_file']['name'],
                 "filter_type": imagearray['_file']['type'],
                 "pic_id": "d5bd"
             };

             Album.album_pics.push(newPics);
             // }

            $scope.albarr = [];
            $scope.albarr.push(Album);

            if (!JSON.parse(localStorage.getItem('album'))) {
                localStorage.setItem('album', JSON.stringify($scope.albarr));               
            } else {

                $scope.getalb=JSON.parse(localStorage.getItem('album'));
                for(irf in $scope.getalb) {
                    console.log('$scope.getalb');
                    console.log($scope.getalb[irf]);


                        $scope.album.album_desc = AlbumDescription;
                         //$scope.album.id= val['id']+1;
                        $scope.album.album_title= AlbumTitle;                
                         $scope.album.ImageName= ImageName;
                         $scope.album.youtube1= youtube1;
                         $scope.album.youtube2= youtube2;
                         $scope.album.videocomment1= videocomment1;
                         $scope.album.videocomment2= videocomment2;                      
                         $scope.album.album_pics= [{
                             "media_type": imagearray['_file']['type'],
                             "last_modified_date": imagearray['_file']['lastModified'],
                             "thumnail_pic_loc": imagearray['_file']['name'],
                             "large_pic_loc": imagearray['_file']['name'],
                             "filter_type": imagearray['_file']['type'],
                             "pic_id": "d5bd"
                         }];


                    $scope.getalb[irf]=$scope.album;
                    $scope.albarr.push($scope.getalb[irf]);
                }
                console.log($scope.albarr);
            localStorage.setItem('album', JSON.stringify($scope.albarr));
            //$route.reload();


            }


        }

现在结果

[{
    "album_desc": "test2",
    "id": 1,
    "album_title": "Test1",
    "ImageName": "Penguins.jpg",
    "album_pics": [{
        "media_type": "image/jpeg",
        "last_modified_date": 1247549551000,
        "thumnail_pic_loc": "Penguins.jpg",
        "large_pic_loc": "Penguins.jpg",
        "filter_type": "image/jpeg",
        "pic_id": "d5bd"
    }]
}, {
    "album_title": "Test1",
    "album_desc": "test2",
    "ImageName": "Penguins.jpg",
    "album_pics": [{
        "media_type": "image/jpeg",
        "last_modified_date": 1247549551000,
        "thumnail_pic_loc": "Penguins.jpg",
        "large_pic_loc": "Penguins.jpg",
        "filter_type": "image/jpeg",
        "pic_id": "d5bd"
    }]
}]

我的预期输出

    [{
    "album_desc": "test1",
    "id": 1,
    "album_title": "Test1",
    "ImageName": "Light.jpg",
    "album_pics": [{
        "media_type": "image/jpeg",
        "last_modified_date": 1247549551000,
        "thumnail_pic_loc": "Light.jpg",
        "large_pic_loc": "Light.jpg",
        "filter_type": "image/jpeg",
        "pic_id": "d5bd"
    }]
}, {
    "album_title": "Test1",
    "album_desc": "test2",
    "ImageName": "Penguins.jpg",
    "album_pics": [{
        "media_type": "image/jpeg",
        "last_modified_date": 1247549551000,
        "thumnail_pic_loc": "Penguins.jpg",
        "large_pic_loc": "Penguins.jpg",
        "filter_type": "image/jpeg",
        "pic_id": "d5bd"
    }]
}]

【问题讨论】:

  • 只是一个旁注......使用 for( ... in ... ) 在 javascript 中迭代数组并不是一种好的做法。
  • 您只是在寻找Array.push 吗?
  • 我不知道,如何解决这个问题?
  • 是的@adeneo 我正在使用相同但得到错误的结果我该如何解决这个问题?
  • $scope.albarr.push(Album); 移动到 localStorage 之后的函数末尾。

标签: javascript jquery arrays angularjs


【解决方案1】:

如果我了解您的问题,您是否只想在每次单击表单时在 localStorage 数组中添加一个新专辑?并且您的代码当前每次都存储同一张专辑。

对于这个简单的操作,您的代码真的很复杂!我不明白你的“for”循环的目的?

对我来说,这是解决方案:

$scope.getalb = JSON.parse(localStorage.getItem('album'));

if (!JSON.parse($scope.getalb)) {
  localStorage.setItem('album', JSON.stringify($scope.albarr));
} else {
  $scope.getalb.push(Album);
  localStorage.setItem('album', JSON.stringify($scope.getalb));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2021-04-05
    • 1970-01-01
    相关资源
    最近更新 更多