Json 如何在Angularjs中动态添加新元素

Json 如何在Angularjs中动态添加新元素,json,object,angularjs,angularjs-ng-repeat,Json,Object,Angularjs,Angularjs Ng Repeat,我有一个ng repeat循环查看我的对象值。 然后我想用一个按钮将新的空白元素添加到ng repeat值的最后一个 我怎样才能做到这一点 我的数据是json对象。我试过了 In controller $scope.objs = {'a': 'a', 'b':'b'}; In view {{Object.keys(objs).length}}; But nothing show in view. 更新 <div ng-repeat="docstep in docs.docsteps"

我有一个
ng repeat
循环查看我的对象值。 然后我想用一个按钮将新的空白元素添加到
ng repeat
值的最后一个

我怎样才能做到这一点

我的数据是json对象。我试过了

In controller
$scope.objs = {'a': 'a', 'b':'b'};

In view
{{Object.keys(objs).length}};
But nothing show in view.
更新

<div ng-repeat="docstep in docs.docsteps" class="docstep">
   {{docstep.text}}
</div>

{{docstep.text}
然后我想得到物体的长度,这样我就可以了。点击按钮中的length+1
但我不知道如何计算物体的长度。或者有更好的主意吗?

我把你的
ng repeat
拿了出来。请注意,我将您的对象放在
$rootScope
中,但您可以将同一对象应用于ng repeat所在的任何范围

JS
jsIDLE:

使用ng click将单击处理程序绑定到按钮:

<div ng-repeat="docstep in docs.docsteps" class="docstep">
   <input type="text" value="{{docstep.text}}">
</div>
<button ng-click="addNew()">Add another input</button>
When this button is clicked. It will add another blank input
<br>Which the new input will be docstep3

注意:您应该使用ng app来定义angular的工作区,使用控制器来驻留模型(文档),并定义视图的行为(添加新的

这是你所有的视图代码还是更多?你的ng重复是什么样子的?@MikeRobinson我更新了question@mitch我更新了question@szafiddle补充道,请检查答案的注释。我根据我的实际数据修改了你的fiddle,并告诉你我想要什么。请检查一下<代码>docstep1、docstep2在实际数据中是随机的。我只是为了测试而硬编码。顺便问一下,可以检查视图中的长度吗?@vzhen您需要在对象上循环才能知道长度。I 循环。但我不知道如何计算物体的长度。没有数组。我尝试了
{{Object.keys(docs.length}}
,但不起作用。如果假设您在docsteps中有docstep1和docstep2,那么您期望得到什么?长度=2?在上面的代码中,c指向您看到的对象的长度。您不需要在对象上循环。不过,如果您希望循环它,那么您可以在这里查看:docstep1和docstep2只是测试数据。在现实生活中,我不知道会有多少步骤。所以我必须在上面循环,知道长度。您给出的示例是关联数组。
<div ng-repeat="docstep in docs.docsteps" class="docstep">
   <input type="text" value="{{docstep.text}}">
</div>
<button ng-click="addNew()">Add another input</button>
When this button is clicked. It will add another blank input
<br>Which the new input will be docstep3
var myApp = angular.module('myApp',[]);
myApp.run(function($rootScope){

    $rootScope.docs = {
        "docsteps" : {
            "docstep1" : {
               "text" : "a"
            },
            "docstep2" : {
               "text" : "b"
            }
        }

    }

    var c = 2; 
    $rootScope.addNew = function(){
        count++;
        $rootScope.docs.docsteps["docstep"+count] = {"text":count}
    }
});