需要使用角度节点js存储动态表单字段值的数据

时间:2015-12-22 12:29:34

标签: mysql angularjs node.js

我有一个基于该字段的2个静态输入字段用户可以添加更多字段,这些字段具有项目名称,项目描述,项目计数和2个组合框用户可以通过单击添加字段添加任意数量的字段我不需要将2个静态输入字段值存储在一个mysql表中并将动态字段值存储在另一个mysql表中,我已经在节点js中编写了myswl insert查询但是静态和动态字段值都没有存储而是存储未定义,我将发布我的node,html,angular js code下面请更正我

HTML

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script type="text/javascript" src="okay.js"></script>
</head>
<body ng-app="testApp">
<div ng-controller="MainCtrl">
<form name="frm" class="form-inline" novalidate>
  <div class="form-group">
    <input type="string" name="cat_name" class="form-control" ng-model="choices.cat_name" placeholder="Enter Category Name" ng-pattern="/^[a-zA-Z]*$/" required>
  </div>
  <div class="form-group">
    <input type="text" name="cat_desc" class="form-control" ng-model="choices.cat_desc" placeholder="Enter Your Description" ng-pattern="/^[a-zA-Z]*$/" required>
  </div>
  <div class="form-group">
    No of Item Count<input type="disable" class="form-control"  name="count" ng-disabled="true" ng-model="choices.length">
  </div>
  <br>
  <fieldset data-ng-repeat="choice in choices track by $index" >
    <br>
    <div class="form-group">
      <input type="text" ng-model="choice.itemName" class="form-control" name="item_name" placeholder="Category Item Name" ng-pattern="/^[a-zA-Z]*$/" required>
    </div>
    <div class="form-group">
      <input type="text" ng-model="choice.itemDesc" class="form-control" name="item_desc" placeholder="Category Description" ng-pattern="/^[a-zA-Z]*$/" required>
      </div>
<div class="form-group">
      <input type="number" ng-model="choice.itemView" class="form-control" name="item_count" ng-pattern="/^[0-9]/" placeholder="Category Item View Count" required>
      <p class="help-block" ng-show="frm.item_count.$error.pattern">numbers only allowed</p>
     State:<select id="country" ng-model="choice.states" name="state" ng-options="country for (country, states) in countries">
    </div>
      <div class="form-group"> 
    <option value=''>Choose</option>
        </select> City:
        <select id="state" ng-disabled="!choice.states" name="city" ng-model="one">
            <option value="">Choose</option>
            <option ng-repeat="state in choice.states" value="{{state.id}}">{{state.city}}</option>
        </select>
     </div>
    <button ng-click="removeChoice()" class="remove btn btn-danger" >close</button>
  </fieldset>
  <br>
  <button class="addfields btn btn-success" ng-click="addNewChoice()" ng-disabled="!frm.cat_name.$dirty||!frm.cat_desc.$dirty||frm.cat_desc.$invalid||frm.cat_name.$inavalid">Add fields</button>
 <button class="addfields btn btn-success" ng-disabled="!frm.cat_name.$dirty||!frm.cat_desc.$dirty||frm.cat_desc.$invalid||frm.cat_name.$inavalid||!frm.item_name.$dirty||frm.item_name.$invalid||!frm.item_desc.$dirty||frm.item_desc.$invalid||!frm.item_count.$dirty||frm.item_count.$invalid||!frm.state.$dirty||!frm.city.$dirty" ng-click="submit()">Save</button>
  <span class="help-block" style="color:red" ng-show="frm.cat_desc.$error.pattern">ERROR:<BR/>text only allowed</span >
  <span class="help-block" style="color:red" ng-show="frm.item_desc.$error.pattern">ERROR:<BR/>text only allowed</span >  
  <span class="help-block" style="color:red" ng-show="frm.cat_name.$error.pattern">ERROR:<BR/>text only allowed</span >
  <span class="help-block" style="color:red" ng-show="frm.item_name.$error.pattern">ERROR:<BR/>text only allowed</span >
 </form>
</div>
</body>
</html>

Angular(好的)

var app=angular.module('testApp', []);
  app.controller('MainCtrl', function($scope,$http) {
  $scope.choices = [{}];
  var newItemNo;
  $scope.addNewChoice = function() {
    newItemNo = $scope.choices.length+1;
    $scope.choices.push({'id':'choice'+newItemNo});

}
  $scope.no=newItemNo;
  $scope.removeChoice = function() {
    $scope.choices.splice(this.$index,1);
  };

$scope.submit=function(category){
$http({method:"post",url:"http://localhost:3000/insert",data:category})
 .then(function(data){
                /* Success callback */
            alert("Data hase been entered!");
                // console.log("success");
            },function(err){
                /* Failure callback */
           alert("Something went wrong!");
            });
}

     $scope.countries = {
         'Andhra': [{
             'id': '01',
             'city': "Guntur"
         }, {
             'id': '02',
             'city': "Hyderabad"
         }],
             'Tamilnadu': [{
             'id': '03',
             'city': "CBE"
         }, {
             'id': '04',
             'dep': "Trichy"
         }]
     };
});

节点(服务器)

var mysql=require('mysql');
var http=require('http');
var uuid = require('node-uuid');
var path=require('path');
var express=require('express');
var app=express();
var bodyparser=require('body-parser');
app.use(bodyparser.json());       
app.use(bodyparser.urlencoded({extended: true})); 
var myconnection = mysql.createConnection({
host : "localhost",

user : "root",

password : "",

database : "siva"
});
app.use(express.static(__dirname + ""));
var uqid= uuid.v1();
var it_id=uuid.v4();
var tt=1;
var status="active";
app.post("/insert",function(req,res){
    console.log(req.body);
    /* TODO: Now just check that your drive function is correct, SQL is correct and whether what arguements passed to SQL callback is correct */
    myconnection.query('Insert into cate_tbl (cat_id,cat_name,cat_desc,cat_view_count,cat_status) VALUES ("'+uqid+'","'+req.body.cat_name+'","'+req.body.cat_desc+'","'+tt+'","'+status+'")',function(err, results, fields) {

        if (err) {console.log("DB Error"+err); res.send("add failed"+err);}
        else res.send("add success");
    });
      myconnection.query('Insert into cate_item (cat_it_id,cat_it_name,cat_pid,cat_it_count,cat_it_desc,cat_it_status) VALUES ("'+it_id+'","'+req.body.itemName+'","'+uqid+'","'+tt+'","'+req.body.itemDesc+'","'+status+'")',function(err, results, fields) {
        //if (err) throw err;
        if (err) {console.log("DB Error"+err); res.send("add failed"+err);}
        else res.send("add success");
    });
});
app.get('/',function(req,res){
    res.sendfile("index.html");
});

app.listen(3000,function(){
    console.log("It's Started on PORT 3000");
})

0 个答案:

没有答案