What is the proper way to declare calculated fields in AngularJs' model? -


how should declare calculated field in angularjs model? if this:

 $scope.model = {         id: null,         calculatedfield: function() {             return 1+2;         }     }; 

and send whole object web server this:

$http.post(url, $scope.model)... 

it sends null calculatedfield field value. seemingly expects properties, not functions while serializing object json.

update: want calculatedfield() being automatically called every time model serialized in json

if calculatedfield should behave value make getter:

$scope.model = {     id: null,     calculatedfield() {         return 1+2;     } }; 

you can use regular value property except can't assign value it. of course, gets serialized properly.

var x = $scope.model.calculatedfield; // x = 3  $scope.model.calculatedfield = 4; //doesn't change calculatedfield  json.stringify($scope.model); // "{"id":null,"calculatedfield":3}" 

getters regular javascript btw.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -