0

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>

次に、オブジェクトの長さを取得したいので、ボタンのクリックで .length + 1 を実行できますが、オブジェクトの長さを取得する方法がわかりません。それとももっと良いアイデアがありますか?

4

2 に答える 2

1

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

JS は次のようになります。

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}
    }
});

注: ng-appを使用して角度の作業領域を定義し、コントローラーを使用してモデル ( docs ) を常駐させ、ビューの動作を定義 ( addNew ) する必要があります。

于 2013-08-08T19:09:57.593 に答える