简体   繁体   English

在MEAN应用程序中处理复选框

[英]Handling checkboxes in MEAN application

I'm very new to MEAN stack, I have been trying to save an array of check box value and try retrieving it from mongodb.I have clearly defined schema about embedded arrays using mongoose the problem is how to insert the checked items in mongodb. 我对MEAN堆栈非常陌生,我一直试图保存一个复选框值数组并尝试从mongodb中检索它。我已经使用mongoose明确定义了有关嵌入式数组的模式,问题是如何在mongodb中插入选中的项。 my form code goes like: 我的表单代码如下:

<table class="table">
<tr>
    <th><span>Project Name :</span></th>
    <th><input type="text" ng-model="project.projectName" placeholder="Enter projectname"></th>
</tr>
<tr>
    <th>Base Url:</th>
    <th><input type="text" ng-model="project.baseUrl" placeholder="Enter Base URL"></th>
</tr>
<tr>
    <th>Choose APIs:</th>
</tr>
<tr>
    <th><input type="radio" ng-model="project.api" value="downloading" ng-change="downloadAPI()"><span>Downloading API</span></th>
    <th><input type="radio" ng-model="project.api" value="scraping" ng-change="scrapAPI()"><span>Scraping API</span></th>
    <th><input type="radio" ng-model="project.api" value="comparision" ng-change="compareAPI()"><span>Comparision API</span></th>

    <tbody ng-show = "project.api=='downloading'" ng-hide="!downloading">
<tr>
    <th>Choose Files:</th>
</tr>
<tr>
<th><input type="checkbox" ng-model=project.js><span>Js</span></th>
<th><input type="checkbox" ng-model=project.css> <span>Css</span></th>
<th><input type="checkbox" ng-model=project.images><span>Images</span></th>
<th><input type="checkbox" ng-model=project.fonts><span>Fonts</span></th>
<th><input type="checkbox" ng-model=project.multimedia><span>Multimedia</span></th>
</tr>
</tbody>
<tr>
    <th><input type="checkbox" ng-model="project.run" ng-change="runproject()"><span>Run as soon as project is completed!</span></th>
</tr>

<tr>
    <th>
    <div ng-show="action === 'add'"><span><button class="btn btn-success" ng-click="save()">Add Project</button></span></div>
    <div ng-show="action === 'edit'"><span><button class="btn btn-success" ng-click="modify()">Update</button></span>
    </div>

    </th>
</tr>

Every thing is fine with the controller and my post request in the server goes like: 控制器一切正常,我在服务器中的发布请求如下:

//post request
router.post('/',function(req,res,next){
 var files=[];
if(req.body.js != null){
files.push(req.body.js);
}

  if(req.body.css != null){
    files.push(req.body.css);
 }

   if(req.body.images != null){
     files.push(req.body.images);
  }

   if(req.body.fonts != null){
    files.push(req.body.fonts);
   }
   console.log(files);
   var obj= new project(req.body);// new object of the mongoose model

   obj.save(function(err,result){
    if(err){
      console.log(err);
    }
   res.json(result);

}); }); }); });

Is there any other way to store check boxes value in the database? 还有其他方法可以将复选框值存储在数据库中吗? I would highly appreciate your time and suggestions which really means a lot to me. 非常感谢您的宝贵时间和建议,这对我来说确实意义非凡。

When you are passing data from front end use that method 当您从前端传递数据时,请使用该方法

ng-click="modify(project)" ng-click =“ modify(project)”

Data validation part also doing in client side.Use angular required competent for check null input.If you use that then do n't need to check data is null. 数据验证部分也在客户端执行。使用角度所需的能力来检查空值输入。如果使用该角度,则不需要检查数据是否为空。

files.push(req.body) files.push(需求主体)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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