0

I have a ng-repeat to loop my object value to view. Then I want to have a button to add new blank element to the last of ng-repeat value.

How can I do this in angular?

My data is json object. I tried

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

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

Update

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

Then I want to get the length of objects so I can .length + 1 in the button click But I have no idea how to get objects length. Or is there any better idea?

7
  • Is that all of your view code or is there more? Commented Aug 8, 2013 at 17:58
  • what does your ng-repeat look like? Commented Aug 8, 2013 at 17:59
  • Do you have an object docs that is an array of objects in a property docsteps? Commented Aug 8, 2013 at 18:13
  • No, they are object object not array @mitch Commented Aug 8, 2013 at 18:15
  • 1
    @sza fiddle added, please check answer's comment Commented Aug 8, 2013 at 18:50

2 Answers 2

1

Bind a click handler to the button using 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

This is how your JS should look:

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

NOTE: You should use ng-app to define work area for angular and use controllers to reside the models(docs) and define the behaviour of your view (addNew).

Sign up to request clarification or add additional context in comments.

6 Comments

docstep1, docstep2 will be random in real data. I just hard code for testing purpose. By the way, is that possible to check the length in view?
@vzhen You need to loop over the object to know the length.
I looped. But I have no idea how to get the length of objects. There is not array. I tried {{Object.keys(docs).length}} but not working.
If lets say you have docstep1 and docstep2 in docsteps then what do you expect? length = 2? In the above code, c points to the length of the object you see. You don't need to loop over the object. Still if you wish to loop over it then you can have a look here: stackoverflow.com/questions/5223/…
the docstep1 and docstep2 are just testing data. In real life, i cannot know how many docsteps will be there. So I have to loop over it and know length. The example you gave is associate array.
|
0

I took your ng-repeat and made it work. Notice I put your object in the $rootScope but you can apply the same object to any scope that your ng-repeat is in.

JS

var myApp = angular.module('myApp',[]);
    myApp.run(function($rootScope){
    $rootScope.docs={docsteps:[{text:'A'},{text:'B'},{text:'C'}]};
});

JSFIDDLE: http://jsfiddle.net/mac1175/Snn9p/

1 Comment

I modified your fiddle to my actual data and tell what I want. Please check it out. jsfiddle.net/EvsGw/2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.