0

I am trying to to update the attribute of an onbject in angularjs array of object.

I have the folowing array of object:

  $scope.formes= [
{id: 0, geo: 0,cx:0, cy:0, b:0, h:0, r:0, d:0, a:0, ax:0, ay:0, val: 0}
 ];

The values of each attribute are set to 0 by default until the user type in a new value in a field. Some values are updated after the user add a new objet or hit a button. This app is used to calculate the center of basic geometric shapes and there moments. Just fyi.

This is the function that is running when the user add an object.

$scope.ajoutForme = function(){

     $scope.formes.a = parseInt($scope.formes.b) * parseInt($scope.formes.h); /// not working

  $scope.formes.push({
    id: $scope.nbrForme++ //just adding and new id
  });  
}

Before I add the objet I want to update some values with calculations. For exemple in this case I what to set the value of a with b*h.

I have try

$scope.formes[nbrForme].h = parseInt($scope.formes[nbrForme].b) * parseInt($scope.formes[nbrForme].h); //This is working but only the first time I press the button??

nbrForme is = to the id of the element I am working on and gets incremented when I add a new object.

Complete Controler

var moment = angular.module('moment',[]);
var nbrForme = 0;

moment.controller('momentCtrl', ['$scope', function($scope) {
  $scope.nbrForme = nbrForme;
  
 $scope.formes= [
{id: 0, geo: 0,cx:0, cy:0, b:0, h:0, r:0, d:0, a:0, ax:0, ay:0, val: 0}
 ];


$scope.ajoutForme = function(){

	 $scope.formes[nbrForme].a = parseInt($scope.formes[nbrForme].b) * parseInt($scope.formes[nbrForme].h); /// only work once
	
  $scope.formes.push({
	id: $scope.nbrForme++
  });  
}
}
}]);

2
  • Please provide some more code... Where are you initializing the $scope.object? inside a controler? If so how does the controller look like and where / how are you manipulation the attr3 attribute? Inside a method? If so is the method called? Commented Mar 12, 2016 at 22:10
  • Updated with my actual code Commented Mar 12, 2016 at 22:24

1 Answer 1

2

Your object definition is wrong:

$scope.object = [
    {id:0, attr1:2, attr2:4, attr3:0},
    {id:1, attr1:2, attr2:4, attr3:0},
    {id:2, attr1:2, attr2:4, attr3:0}
];

Note a added , to separate the elements of the array.

Edit:

You wrote that this line doesn't work $scope.formes.a = parseInt($scope.formes.b) * parseInt($scope.formes.h); This is because $scope.formes is an array, so you must reference to a specific object inside the array that has the a/b/h property. The question is which one?

If it's the first index in the array you'll do

$scope.formes[0].a = parseInt($scope.formes[0].b) * parseInt($scope.formes[0].h);

The last element:

var lastIndex = $scope.formes.length - 1;
if (lastIndex >= 0) {
    $scope.formes[lastIndex].a = parseInt($scope.formes[lastIndex].b) * parseInt($scope.formes[lastIndex].h);
}

If $scope.nbrForme is the ID of the element you're currently working on, then you need to decrease its value by 1, because you start with the value of 1, and the first index of an array is 0:

$scope.formes[$scope.nbrForme - 1].a = parseInt($scope.formes[$scope.nbrForme - 1].b) * parseInt($scope.formes[$scope.nbrForme - 1].h);
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah sorry i have updated my code with the actual code
It's illegal to do $scope.formes.a because $scope.formes is an array. So you must reference to a specific index inside that array. Can you show us the complete code of the controller?
Thank you sir this is working. I forgot to add $scope in front of nbrForm

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.