0

How to add object in array and show it on web-page?

http://plnkr.co/edit/19w1Q3XhoWQcpxm5SuxX?p=preview

$scope.add = function() {
$scope.items.push($scope.item);
$scope.item = '';

};

When I try to add item it's not show on page

1
  • 1
    Just change the first line of you dd function to $scope.items.push({'item':$scope.item}); so the item object will have the same structure of data than the others. Commented Jun 27, 2016 at 9:28

2 Answers 2

3

Just change your push code to:

$scope.items.push({item: $scope.item});

You are trying to push single string to an Array of objects.

you can also change your model in controller to

$scope.item = {text: ""}

and on your view change ng-model to

ng-model="item.text"
Sign up to request clarification or add additional context in comments.

Comments

2

Hi just edit your function

because its an object , u have to create object and need to push inside an array

$scope.add = function() {
    var obj ={};
    obj.item = $scope.item;
    $scope.items.push(obj);
    $scope.item = '';
  };

Comments

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.