简体   繁体   中英

Save , Send and Retrieve data in Angularjs

I am trying to make a news object and in that section in which there are separate sub-section so it will look like this. user can add and remove subsection. 图片

and when I press the save button it should send the data as following json structure seperating each subsection by ||.

{"news": {
    "section1": ["ABCDE||FGHI||JKLM"],
    "section2": ["NOPQ"],
    "section3": ["RSTU"]
  }
}

and when use save the data it should get saved and when the user open that page again it should be as last saved. This is what I have tried so far. I have tried to make a div and then wrap test area in it in ng-repeat but it seems like it should be it a table.

// For adding the subsection
$scope.section1 = [];
    $scope.addsection1=function(){
    $scope.section1.push({});
}

// For removing the subsection
$scope.removesection1 = function(id){
    var indexToRemove;
    for(i = 0; i < $scope.section1.length; i++){
        if($scope.section1[i].id === id){
            indexToRemove = i;
        }
        $scope.section1.splice(indexToRemove, 1);
    }
}
<div class="section-div flex-column">
    <div class="flex-row">
        <h4 style="flex-grow: 2;">New Updates</h4> 
        <button class="add-btn" ng-click="addsection1()">+Add field</button>
    </div>
    <textarea ng-repeat="section in section1"
              style="margin: 7px;border: 1px solid #00000047;border-radius: 4px;"
              name="" id="">
    </textarea>     
</div>

Please help me I ma beginner in the angularjs. Thanks in advance.

send the data as following json structure seperating each subsection by ||

One approach is to use array.join :

 var arr = ["ABCDE", "FGHI", "JKLM"]; var obj = { news: { section1: [arr.join("||")] } }; console.log(obj);

Conversely, to receive, use string.split :

 var obj = { news: { section1: ["ABCDE||FGHI||JKLM"] } }; var arr = obj.news.section1[0].split("||"); console.log(arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM