提取自动递增的id

时间:2016-07-13 01:56:49

标签: javascript

我有这段代码;

$scope.hold = {};

$scope.save = function (){
    var data = {
        parent_id: $scope.hold.id, //-->This is an auto generated id.
        name: $scope.hold.some_name,
        address: $scope.hold.address,
        dependent = {
                     parent_id: $scope.hold.id, //--> This is equal to the id above. 
                     child_name: $scope.hold.child_name,
                     child_age: $scope.hold.child_age
        }
    }
myService.saveData(data, function(){
})
}

现在,我无法获得自动增加的ID,我也无法记录它。它返回undefined。从哪里弄错了?我如何才能获得自动增加的ID?这是为了在我的api中完成我的查询。

dependent内的var data将保存到另一个模型中,该模型的外键为id

我如何能够提取自动递增的id(父表),以便我可以将其初始化为我的孩子id(从属表)以执行单个 HTTP POST 方法。

桌面设计(样本)

**Parent Table** parent_id | name | address 1 | john | place

**Dependent Table** dependent_id | parent_id | name | age 2 | 1 | james| 7

2 个答案:

答案 0 :(得分:3)

您的代码的问题是您正在创建的JSON对象。

应该如下所示

var data = {
    id : $scope.hold.id, //-->This is an auto generated id.
    name : $scope.hold.some_name,
    address : $scope.hold.address,
    dependent : {
                 id:$scope.hold.id, //--> This is equal to the id above.
                 child_name:$scope.hold.child_name,
                 child_age:$scope.hold.child_age
    }
}

片段。

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {
 
 $scope.hold = {};
 
 $scope.hold.id = Math.floor((Math.random() * 10) + 1);

$scope.save = function (){
    var data = {
        id : $scope.hold.id, //-->This is an auto generated id.
        name : $scope.hold.some_name,
        address : $scope.hold.address,
        dependent : {
                     id:$scope.hold.id, //--> This is equal to the id above.
                     child_name:$scope.hold.child_name,
                     child_age:$scope.hold.child_age
        }
    }
    
alert(data.dependent.id);    
    
}

});
 

 <html ng-app="app">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>dynamic ID {{hold.id}}</p>
     <a ng-click="save()"> click here to save</a>
  </body>

</html>

答案 1 :(得分:0)

另一种方式呢?

function Man(name, age)
{
  this._id = Math.round(Math.random() * 8999 + 1000);
  this._name = name || null;
  this._age = age || 0;
  this._address = null;
  this._dependent = null;
}

Man.prototype.getId = function()
{
  return this._id;
}

Man.prototype.getName = function() 
{
  return this._name;
}

Man.prototype.setName = function(name)
{
  this._name = name;
  return this;
}

Man.prototype.getAge = function()
{
  return this._age;
}

Man.prototype.setAge = function(age)
{
  this._age = age;
  return this;
}

Man.prototype.getAddress = function()
{
  return this._address;
}

Man.prototype.setAddress = function(address)
{
  this._address = address;
  return this;
}

Man.prototype.getDependent = function()
{
  return this._dependent;
}

Man.prototype.isDependent = function()
{
  return this._dependent ? true : false;
}

Man.prototype.setDependent = function(dependent)
{
  this._dependent = dependent;
  return this;
}

Man.prototype.logInfo = function()
{
  console.log('id: ' + this._id);
  console.log('name: ' + this._name);
  console.log('age: ' + (this.age > 0 ? this.age :  '-'));
  console.log('address: ' + (this._address ? this._address : '-'));
  console.log('dependent: ' + (this._dependent ? this._dependent.getId() : 'no'));
}



var Jane = new Man('Jane', 41);
Jane.setAddress('NY, Miracle str., 5');
var Max = new Man('Max', 20);
Max.setAddress(Jane.getAddress()).setDependent(Jane);
Jane.logInfo();
Max.logInfo();