angularjsangularjs-forms

AngularJS Edit JSON data using forms


In AngularJS , I am trying to make and editable TreeView from JSON data. I want to get a nodevalue from JSON and edit it using forms. While editing, the original JSON should change immediately. I created below plunkr for this but not getting an idea, how to achieve this.

Plunkr

var app = angular.module('plunker', []);

app.directive('collection', function () {       
    return {
        restrict: "E",
        //replace: true,
        scope: {collection: '='},
        //controller: 'TreeController',
        //bindToController: true,       
        template: "<ul><member ng-repeat='member in collection' member='member'></member></ul>"         
    }
})

app.directive('member', function ($compile) {
    var linkerfunc = function(scope, element, attrs) {  
                    var collectionSt = '<collection collection="member.children"></collection>';
                    $compile(collectionSt)(scope, function(cloned, scope)   {                                           
                        element.append(cloned); 
                     });
                     scope.ShowDetailsFunc = function() {   
                       console.log('in function scope showdetails')
                        scope.ShowDetailsCtrlFunc(element,event);                     
                    } 
    }
    return {
        restrict: "E",
        //replace: true,
        scope: {member: '=', ShowDetailsCtrlFunc : '&'},
        template: "<li><span ng-click=ShowDetailsCtrlFunc($event)>{{member.NodeName}}</span></li>",     
        controller: 'MainCtrl',
        //controllerAs: 'MainCtrl',
        //bindToController: true,
        link: linkerfunc        
    }
})

app.controller('MainCtrl', function ($scope,$timeout) {     

    $scope.Intialfunc = function() { 
        $scope.testdata = []
        var myjsondata = JSON.parse('{ "NodeName": "Parent", "NodePath": "1", "children": [ { "NodeName": "mychild", "NodePath": "1.1", "children": [ { "NodeName": "chld1", "NodePath": "1.1.1", "children": [] } ] } ] }');
        $scope.testdata.push(myjsondata);
            //console.log($scope.testdata) //This one is showing
            $scope.changename = $scope.testdata[0].children[0].children[0].NodeName;    

        }   

    $timeout(function(){ $scope.Intialfunc(); }, 1000)

    $scope.ShowDetailsCtrlFunc = function(event) {
            console.log("in function ShowDetailsCtrlFunc"); 

            //event.stopImmediatePropagation(); 
      };
});

I tried angular.copy but that needs the portion of JSON data in a $scope variable and updates that variable as expected.

But my JSON data going to be a huge and i dont know how to update it without using variable. Please help.


Solution

  • You can use ng-change to perform an action when an input changes:

    <input type="text" ng-model="changename" ng-change="inputChange()">
    

    Then, just create a function in your controller that updates what you need it to:

    $scope.inputChange = function() {
      // do stuff here
      $scope.testdata[0].children[0].children[0].NodeName = $scope.changename;
      // write object to JSON
      var JSONstring = $scope.testdata.toJSON();
    }
    

    I would also recommend looking at ng-model-options and changing the debounce settings so that you aren't constantly toJSONing with every keystroke.

    http://plnkr.co/edit/dPWsowm4Puwajgk6CfvA?p=preview