javascript - (Angular) 插入一个对象到 json 数组

标签 javascript json angularjs

我正在尝试将新项目添加到 roleList json 数组。 我试过 push/concat 但它不会更改 roleList 数组。 有什么办法可以解决这个问题?

// The javascript :

function RoleListCtrl($scope)
{
    $('#myTab a[href="#role"]').tab('show');

    $scope.newCompanyName ="";
    $scope.newPosition="";


    $scope.addRole = function()
    {
        var newRole = new function() {
            this.companyName = $scope.newCompanyName;
            this.position    = $scope.newPosition;
            this.id          = '';
        }

        alert("test :"+newRole.companyName);

        $scope.roleList = $scope.roleList.push(newRole);
        // I have also tried this :   $scope.roleList = $scope.roleList.concat(newRole);
    }

    $scope.roleList = [
        {"companyName": "Company 01", "id":"1", "position": "CEO"},
        {"companyName": "Company 02", "id":"2", "position": "Board of Director"},
        {"companyName": "Company 01", "id":"1", "position": "CEO"},
        {"companyName": "Company 02", "id":"2", "position": "Board of Director"}

    ];
}

下面是调用 addRole() 的按钮:

<form class="form-horizontal">
<div id="myModal" class="modal hide fade" ng-controller="RoleListCtrl">

    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Add Role</h3>
    </div>

    <div class="modal-body">

        <div class="control-group">
            <label class="control-label pull-left" for="name">Company Name</label>
            <div class="controls">
                <input type="text" id="coyName" ng-model="newCompanyName" placeholder="Company Name">
            </div>
        </div>

        <div class="control-group">
            <label class="control-label pull-left" for="name">Role</label>
            <div class="controls">
                <input type="text" id="newRole" ng-model="newPosition" placeholder="Role">
            </div>
        </div>

    </div>

    <div class="modal-footer">
        <a href="#" class="btn">Close</a>
        <a href="#" class="btn btn-primary" ng-click="addRole()">Save changes</a>
    </div>

</div>
</form>

<div class="tab-pane" id="role" ng-controller="RoleListCtrl">

                    <a class="btn btn-primary" data-toggle="modal" data-target="#myModal"><i class="icon-plus icon-white"></i>Add New Role</a>
                    <BR>

                    <table class="table table-bordered table-white spacer5">
                        <tr>
                            <th>company name</th>
                            <th>position</th>
                            <th>action</th>
                        </tr>

                        <tr ng-repeat="eachRole in roleList">
                            <td>{{eachRole.companyName}}</td>
                            <td>{{eachRole.position}}</td>
                            <td>
                                <button ng-click="deleteRole($index)">delete</button>
                            </td>
                        </tr>

                    </table>
                    <BR>

                </div>

最佳答案

这一行是你的问题:

$scope.roleList = $scope.roleList.push(newRole);

当您调用 push 时,它会返回长度 ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push )。您实质上是将新 Role 插入其中,然后将 roleList 替换为数组的长度,从而丢失数组。

关于javascript - (Angular) 插入一个对象到 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17416548/

相关文章:

Javascript 从页面加载开始计数 - 需要使用 ajax 重置

java - 如何使用 JSR-223 访问导致 ScriptException 的 Java 异常

c# - 在 WCF Json 序列化中包含类名称

JSON Schema 多重从属关系

javascript - 无法连接AngularJS中的服务功能

javascript - SweetAlert 警报显示部分 View

Javascript 简单对象默认覆盖

java - 如何在 Eclipse 中使用 gson 将 ResultSet 查询转换为 JSON?

angularjs - 可能未处理的拒绝 : {}

javascript - AngularJS:如何在指令中返回带有新同级元素的原始元素?