简体   繁体   中英

making calculation on property of the scope doesn't work

I'm very very new to Angular js concept so I have trouble with a calculation. I have a form where I'm expecting from user to enter a volume number. I would like to make a calculation and the result of this calculation will be a field for my mongo document. I can create mongo document but my calculation doesn't work. It always insert the original product volume, not the one after the calculation.

This is the object that will be a mongo document. I can fill types and tags array without problem.

$scope.product = {
    types: [],
    tags: []
  };

Form group where I retrieve product volume

.form-group
        label.col-sm-3.control-label Volume
        .col-sm-4
            input.form-control(
            ng-model='product.volume',
            type='number',
            placeholder='Volume')

I have a submit button in my controller. Inside this submit button I'm creating a new product document for mongo and filling the values. I'm also doing my calculation inside this submit function.

var product = new Product($scope.product);

var capacity = 100
$scope.product.volume = (capacity / $scope.product.volume);

Finally after finishing my mongo document, I'm saving it.

product.$save(function ()...

What might be the problem?

It seems like you are creating the Product, and then editing the $scope.product. You should do the transformation on $scope.product.volume before creating the Product.

var product;
var capacity = 100;

$scope.product.volume = (capacity / $scope.product.volume);
product = new Product($scope.product);
product.$save(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM