第二项将覆盖第一个值

时间:2015-08-10 13:54:32

标签: html angularjs

我创建了管理项来管理下拉值。每次用户添加新项目时我都会遇到问题,但是当他/她添加第二项时,它会覆盖视图级别第二项的第一个值

的JavaScript

(function () {
    'use strict';

    var app = angular.module('ItemsManagement');

    var ItemsManagementController = function ($scope, $location, authService, userInfoService, itemsManagementService) {

        var username = '';
        $scope.model = formValueModelTemplate();
        $scope.displayModel = rrnFormModelTemplate();

        $scope.editingMode = [];
        $scope.backup = [];
        $scope.newValueItem = ' ';


        $scope.addValue = function (parentId) {
        $scope.model.formControllerID = parentId;
        $scope.model.createdBy = username;
        $scope.model.CreatedOn = new Date();
        $scope.model.modifiedBy = username;
        $scope.model.modifiedOn = new Date();
        $scope.model.formControllerValueName = $scope.newValueItem;
        $scope.displayModel.rrnConditionsValues.push($scope.model);
        itemsManagementService.postValue($scope.model);
        $scope.newValueItem = '';
        };
    };

    app.controller('ItemsManagementController', ['$scope', '$location', 'authService', 'userInfoService', 'itemsManagementService', ItemsManagementController]);

}());

html

<div class="panel-body">
    <ul>
        <li data-ng-repeat="value in displayModel.rrnConditionsValues track by $index" data-ng-hide="value.deleted">

            <div id="showItem" data-ng-show="!editingMode[$index]">
                <input class="" type="submit" value="Edit" data-ng-click="toggleEdit($index)">
                <input class="" type="submit" value="Delete" data-ng-click="deleteValue($index)">
                <label>{{value.formControllerValueName}}</label>
            </div>
            <div id="editItem" data-ng-show="editingMode[$index]">
                <input class="" type="submit" value="update" data-ng-click="updateValue($index)">
                <input class="" type="submit" value="Cancel" data-ng-click="cancel($index)">
                <input type="text" size="30" data-ng-model="value.formControllerValueName" placeholder="add new here">
            </div>
        </li>
    </ul>
</div>

1 个答案:

答案 0 :(得分:0)

由于您的model初始化仅调用一次,因此请尝试添加:

$scope.addValue = function (parentId) {
    $scope.model = formValueModelTemplate(); // <- Add this line
    $scope.model.formControllerID = parentId;
    $scope.model.createdBy = username;
    $scope.model.CreatedOn = new Date();
    $scope.model.modifiedBy = username;
    $scope.model.modifiedOn = new Date();
    $scope.model.formControllerValueName = $scope.newValueItem;
    $scope.displayModel.rrnConditionsValues.push($scope.model);
    itemsManagementService.postValue($scope.model);
    $scope.newValueItem = '';
};

您的model是一个对象。当调用函数addValue时,model已经存在。因此,在model上设置属性将覆盖现有值。结果与:

相同
$scope.model.key = valueOne;
$scope.model.key = valueTwo;  // Now valueOne is lost.