如何跟踪AngularJS中生成的对象

时间:2016-06-09 12:08:07

标签: javascript angularjs

所以我有一个动态生成不同输入的表单,它通过将对象附加到数组来实现。

$scope.data = {
    urls: [{}],
    features: [{}],
    testemonies: [{}],
    keywords: [{}],
    tests: [{}],
    newsletters: [{}],
    devFields: [{}],
    devSteps: [{
        id: $scope.devStepsAmount
    }],
    loginFields: [{}],
    steps: [{
        fields: [{}]
    }]
};

$scope.addDevStep = function() {
    $scope.data.devSteps.push({
        id: ++($scope.devStepsAmount)
    });
    console.log($scope.data.devSteps);
};

然后在HTML上我只需使用ng-repeat

来调用它们

<div class="card-body single" ng-repeat="devStep in data.devSteps">

我调用了这个在HTML中添加新对象的函数

<button class="btn btn-simple" data-ng-click="addDevStep()"><i class="fa fa-plus-circle"></i> Add New Step</button>

现在我的问题是我需要能够跟踪生成的每个单独对象并在创建时存储它的ID,你如何在Javascript / Angular中执行此操作?

抱歉这个愚蠢的问题,我更像是一个PHP人员

1 个答案:

答案 0 :(得分:0)

尝试:

var devStepsAmount = 0;
$scope.data = {
    urls: [{}],
    features: [{}],
    testemonies: [{}],
    keywords: [{}],
    tests: [{}],
    newsletters: [{}],
    devFields: [{}],
    devSteps: [{
    }],
    loginFields: [{}],
    steps: [{
        fields: [{}]
    }]
};

$scope.addDevStep = function() {
    devStepsAmount++;
    $scope.data.devSteps['id'] = devStepsAmount;
    console.log($scope.data.devSteps);
};