0

I'm getting some list of data from database, with angular I'm adding it to scope. So on $scope I have list of objects

[Object, Object, Object, Object]   

Where each looks like:

0: "1"
1: "This is my first task"
2: "1"
3: "1"
$$hashKey: "object:3"
active: "1"
id: "1"
priority: "1"
text: "This is my first task"

I basicly try to add new OBJECT to it, with something like:

$scope.tasks.push($scope.newTask);

But this add just string / array not an object, I already tryed to add multiple other way to add object like

$scope.tasks.push("text": $scope.newTask);

But I can't make it work, please may I ask you for help to lead on correct way?

2
  • Please do console.log($scope.newTask); and show us the result. Commented Oct 14, 2015 at 16:41
  • Hey @taxicala it return value from input, so if I write to <input> something like THIS IS IT, it return this as a string Commented Oct 14, 2015 at 16:47

3 Answers 3

3

If it's the value of an input, it will be a string always, you can push an object as follows:

$scope.tasks.push({"text": $scope.newTask});
Sign up to request clarification or add additional context in comments.

Comments

0

Use Json.stingify and pass the result to the push

1 Comment

Hey, I'm not sure if I understand that correctly but If I do: var jsonString = JSON.stringify($scope.newTask); $scope.tasks.push(jsonString); result is same, there is a string added instead of object
0

This is the way to add new objects into array of objects, first you need to access array and then properties of an objcet that are inside it, for example:

 $scope.phonebook = [
       {
           name: 'Johan',
           phone: '622345871',
           email: '[email protected]'
       }
 ];

 $scope.addPerson = function(){    // triger function when submit button is called

       // check against empty fields
       if ( $scope.name != '' ) {  
          // values from input fields
          $scope.phonebook.push({
              name: $scope.name,
              phone: $scope.phone,
              email: $scope.email
          });
          // clear input fields after submission
          $scope.name = '';
          $scope.phone = '';
          $scope.email = '';
      }
  }

hope this will be helpfull, good luck!

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.