【问题标题】:(Angular) insert an object to json array(Angular) 将对象插入到 json 数组中
【发布时间】:2013-07-02 02:35:11
【问题描述】:

我正在尝试将新项目添加到 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>

【问题讨论】:

  • 你不调用.addRole()方法。
  • 我调用了 addRole 并显示了警报...
  • 我非常怀疑你可以这样调用它 - 没有定义全局 addRole() 函数。
  • 它使用角度...如果显示警报,肯定会调用它。我向你保证,问题只在于添加到 json 中。

标签: javascript json angularjs


【解决方案1】:

这一行是你的问题:

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

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

【讨论】:

  • 这是合乎逻辑的,我认为这是这里的问题
  • 好的。我已经尝试过您的建议,并且我注意到实际上角色列表已更改。但是它没有反映在 ng-repeat 中。问题更多在于角度绑定 - 因为它不会反映在 UI 中,但仍不确定是什么原因造成的。
  • 我对 Angular 的了解还不够,甚至无法开始调试它。但我确实做了这个可能会有所帮助的小提琴:jsfiddle.net/J7BYD。我把 .tab 改成了 .tabs 因为我不知道你用的是什么插件。
  • +1 我已经解决了这个问题。我创建了 2 个 ng-controller="RoleListCtrl",这导致 Angular 框架失去了对范围的控制。应该只用同一个控制器包装弹出窗口和东西。最重要的是,我有你指出的问题。感谢您的帮助!
【解决方案2】:
var currentList = $scope.roleList;
var newList = currentList.concat(newRoleArray);
$scope.roleList = newList;

【讨论】:

  • 这对我有用 :) 只是想知道,如何将新对象添加到对象列表的开头而不是末尾?
【解决方案3】:

您可以通过键值对制作对象来直接推送您的对象。

$scope.addRole = function(){
var newRole = {
    "companyName":$scope.newCompanyName,
    "id":"",
    "position":$scope.newPosition
 }
$scope.roleList.push(newRole);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多