2017-02-05 2 views
2

드롭 다운 목록의 값을 설정하는 데 문제가 있습니다. 여기에 내 코드를 붙여 넣습니다.목록 및 값 드롭 다운

HTML 코드 : 내 express.js이 설정되어 마침내 여기

$scope.studentname = ["abc","pqr","xyz"]; 

$scope.save = function(){ 

    $http.post('/savemarks', $scope.setmarks).success(function(data){ 
       console.log("posted data successfully"); 
       console.log(JSON.stringify(data)); 
      }).error(function(data){ 
       console.error("Error posting data"); 
      }) 
} 

그리고 다음은

<form ng-submit="save()"> 
<table style=" table-layout: fixed;" class="table"> 
    <thead> 
    <tr> 
     <th>Section</th> 
     <th>Names</th> 
     <th>Marks</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td>5th class</td> 
     <td> 
     <select id="5thclass" ng-model="setmarks.5thclass" ng-options="val for val in studentname"> 
      <option ng-init="setmarks.5thclass=studentname[0]" selected="selected"></option> 
     </select> 
     </td> 
     <td> 
     <input type="number" name="5thclass" ng-model="setmarks.marks.5thclass"/> 
     </td> 
    </tr> 
    </tbody> 
</table> 
    <div class="sub" class="btn-group"> 
     <input id="savebutton" type="submit" ng-click="Submit" value="Save" class="btn btn-primary"> 
    </div><br /> 
</form> 

내 AngularJS와 코드입니다. 양식 세부 정보를 파일에 저장합니다.

app.post('/savemarks',urlencodedParser,function(req,res){ 
    fs.appendFile(writecost_file,JSON.stringify(req.body,null,1),function(err){ 
    if(err){ 
     console.log(err); 
    }else{ 
     console.log("File saved"); 
    } 
    }); 
}); 

출력은 JSON의 형태 :

{"5thclass":"abc","marks":{"5thclass":66}} 

내가 출력 형식이어야합니다.

{"5thclass":"abc","marks":{"abc":66}} 

마크 란에서 나는 클래스 이름 대신 학생 이름을 원합니다. 아무도 나를 도울 수 있습니까? 감사 .

답변

1

html의 ng-model 지시문에서 setmarks.marks.5thclass를 사용하고 있습니다. 그 때문에 5thclass는 marks 객체의 속성으로 처리됩니다. 그것이 "marks":{"5thclass":66} 으로 주어진 이유입니다. save 이벤트가 발생한 후 컨트롤러에서 수동으로 예상되는 json 객체를 만들어야합니다. 아래. 희망이 당신에게 도움이 될 것입니다.

var st = '{"5thclass" : "' + $scope.setmarks.5thclass + '","marks":{"' + $scope.setmarks.5thclass + '":' + $scope.setmarks.marks.5thclass + '} }'; 
 
var obj = JSON.parse(st);

참고 :이 setmarks.5thclass 같은 속성 첫 번째 문자로 숫자를 사용하지 마십시오. 표현식을 컴파일하는 동안 구문 오류로 간주되어 오류가 발생합니다.

+0

감사합니다. 문제가 해결되었습니다. – naik3

+0

위대한, 그것을 듣고 행복합니다. –

관련 문제