如何从angularjs Form中的json获取数据

时间:2016-09-29 05:31:19

标签: angularjs json

如何在不使用后端的情况下以angularjs形式从json调用数据。我已经编写了这段代码,在这里我无法找到最后从json文件中获取数据的方法。有人请帮助我从这里前进。

$scope.count = $scope.newPreAuth.length;
    };

    //Delete newPreAuth - Using AngularJS splice to remove the preAuth row from the newPreAuth list
    //All the Update newPreAuth to update the locally stored newPreAuth List
    //Update the Count
    $scope.deletenewPreAuth = function (preAuth) {
        $scope.newPreAuth.splice($scope.newPreAuth.indexOf(preAuth), 1);
        getLocalStorage.updatenewPreAuth($scope.newPreAuth);
        $scope.count = $scope.newPreAuth.length;
    };
}]);

//Create the Storage Service Module
//Create getLocalStorage service to access UpdateEmployees and getEmployees method
var storageService = angular.module('storageService', []);
storageService.factory('getLocalStorage', function () {
    var newPreAuthList = {};
    return {
        list: newPreAuthList,
        updatenewPreAuth: function (newPreAuthArr) {
            if (window.localStorage && newPreAuthArr) {
                //Local Storage to add Data
                localStorage.setItem("newPreAuth", angular.toJson(newPreAuthArr));
            }
            newPreAuthList = newPreAuthArr;

        },
        getnewPreAuth: function () {
            //Get data from Local Storage
            newPreAuthList = angular.fromJson(localStorage.getItem("newPreAuth"));
            return newPreAuthList ? newPreAuthList : [];
        }
    };

});

Json代码

PreAuth:
 ======================

URL:http://dev.xxx.com:8080/xxx/preAuth

TYPE:POST

X-Auth-Token    t3Z10HGEiYFdzq9lGtr18ycdeAAXmWBEI64rQAJcAte6Ka8Tz96IAhuXHSgpiKufsd

{
  "preAuth": {
    "claimbId": "newPreAuth",
    "claimbStatus": "new",
    "patientInfo": {
      "patientName": "name",
      "gender": "Male",
      "dob": 950639400000,
      "age": 21,
      "contactNumber": 9987654356,
      "tpaMemberId": 950639400121,
      "policyNumber": "ABC12615627",
      "corporateName": "ABC",
      "EmployeeId": "XYZ10232",
      "otherInsurance": {
        "isOtherInsurance": true,
        "playerName": "xyx",
        "details": "sdfdsafdsfdsf"
      },
      "familyPhysician": {
        "isFamilyPhysician": true,
        "physicianName": "fsdf"'c
        "physicianContactNumber": 7878748728,
        "address": {
          "address1": "Hosa road",
          "address2": "Basapura",
          "city": "Bangalore",
          "state": "Karnataka",
          "country": "India",
          "pincode": "560100"
        }
      },
      "isFamilyPhysician": false,
      "address": {
        "address1": "Hosa road",
        "address2": "Basapura",
        "city": "Bangalore",
        "state": "Karnataka",
        "country": "India",
        "pincode": "560100"
      }
    },
    "medicalInfo": {
      "illnessType": "cancer",
      "clinicalFinding": "description",
      "ailmentDuration": "2 months",
      "ailmentHistory": "description",
      "illnessCause": "alcohol",
      "provisionalDiagnosis": [
        {
          "diagnosisName": "abc",
          "diagnosisICDCode": "121423"
        },
        {
          "diagnosisName": "xyz",
          "diagnosisICDCode": "434543"
        }
      ],
      "differentialDiagnosis": [
        {
          "diagnosisName": "afasdbc",
          "diagnosisICDCode": "12431423"
        },
        {
          "diagnosisName": "fdxyz",
          "diagnosisICDCode": "434sdf543"
        }
      ],
      "clinicalObservations": {
        "BP": "120/80",
        "CVS": "126",
        "P.A.": "abc",
        "R.S.": "aa",
        "CNS": "dd",
        "others": "others"
      },
      "maternityDetails": {
        "maternityType": "G",
        "L.M.P.": 950639400000,
        "E.D.D.": 950639400000
      },
      "accidentDetails": {
        "accidentCause": "xyz",
        "accidentDate": 950639400000,
        "isPoliceComplaint": true,
        "firNumber": "asfsafds"
      },
      "pastIllness": [
        {
          "pastIllnessType": "Diabetes",
          "isPresent": true,
          "NoOfMonth": 2,
          "NoOfYear": 5,
          "illnessSince": 950639400000
        },
        {
          "pastIllnessType": "Hypertension",
          "isPresent": true,
          "NoOfMonth": 2,
          "NoOfYear": 5,
          "illnessSince": 950639400000
        },
        {
          "pastIllnessType": "Other",
          "isPresent": false,
          "NoOfMonth": 2,
          "NoOfYear": 5,
          "illnessSince": 950639400000
        }
      ]
    },
    "treatmentInfo": {},
    "billingInfo": {},
    "documents": [
      {
        "documentId": 12345,
        "documentMetadata": {
          "documentName": "discharge summary",
          "date": 950639400000,
          "version": "1.1",
          "viewedStatus": false,
          "link": "link to view/download document"
        }
      },
      {
        "documentId": 12346,
        "documentMetadata": {
          "documentName": "medical summary",
          "date": 950639400000,
          "version": "1.0",
          "viewedStatus": true,
          "link": "link to view/download document"
        }
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

我创建了样本,它以这种方式工作

     // Code goes here
   var app = angular.module('app',[]);
   app.controller('sample', function($scope,$http){  
   $scope.name = "advaitha";
   $http.get('test.json').then(function(data){
   console.log(data.data);
  });
 })

这里是plunker example

使用HTML5 localStorage会要求您在使用或保存对象之前对其进行序列化和反序列化。

例如:

var myObj = {
firstname: "kisun",
lastname: "rajot",
website: "https://www.kisun.com"
} 
//if you wanted to save into localStorage, serialize it  
window.localStorage.set("empData", JSON.stringify(myObj));  

//unserialize to get object  
var myObj = JSON.parse(window.localStorage.get("empData"));

根据您的代码创建了一个plunker,可以使用您的代码保存和检索json数据。 Please check here