how to make view (form)from json using angular?

前端 未结 1 510
太阳男子
太阳男子 2020-12-12 02:36

I am trying to make view from json .When I have array of objects .I am able to make view and validate that view . If I have this array of object ,in that case I make able t

相关标签:
1条回答
  • 2020-12-12 02:50

    Assuming your JSON is coming from a configuration file or a service, you can start by obtaining the JSON as a JSON object:

    angular.module('myapp', [])
      .controller('MyController', ['$scope', function($scope) {
    
        $scope.outputs = {};
        $scope.rawInput = JSON.parse( '{"studentName": "abc", \
            "input": {\
                "loginUser": {\
                    "displayDetail": "UserId for login.",\
                    "displayName": "Login User Id*",\
                    "inputType": "TEXT",\
    

    (I had to escape returns to allow the pretty printed JSON string to be parsed)

    Once you have that, you are nearly there. Now you can just go the level of JSON that you require and construct your inputs array:

    $scope.formInputs = $scope.rawInput['input'];
    $scope.inputs = [];
    
    angular.forEach($scope.formInputs, function(value, key) {
    /* do something for all key: value pairs */
      $scope.inputs.push({"type":value.inputType.toLowerCase(),"name":value.name,"required": value.required})
    });
    

    Note you should probably do some error checking here - for the purposes of this example, I don't use any. Here is a working plnkr that demonstrates this code.

    I haven't got it all to work - you'll have to construct your select or radio button inputs, but I think you should be able to take it from here.

    EDIT I have undated the plnkr to make it public

    0 讨论(0)
提交回复
热议问题