javascript - How to add some variable to $scope.master object from controller in Angularjs -
my issue how add variable in $scope.master object controller.
i'm saving data db on angular form submit.
and goal automatically save 'date_create' variable on form submit. need have in angular's $scope.master object default. , that, think, need push $scope.master in controller.
what have , doesn't work:
app.controller('addcontroller', ['$scope', '$http', '$location', function($scope, $http, $location) { var currentdate = math.floor(new date().gettime() / 1000); $scope.master = {}; // i've tried add variable here // $scope.master = {date_update: currentdate}; $scope.activepath = null; // function triggers on form submit $scope.addnew = function(product, addnewform) { $http.post('api/add', product).success(function(data){ // , i've tried add here $scope.master.date_update = currentdate; console.log(data); $scope.reset(); $scope.activepath = $location.path('/products'); }); $scope.reset = function() { $scope.product = angular.copy($scope.master); }; $scope.reset(); }; }]); logs:
object {name: "123", category_id: "3", date_update: null, id: "11"}
if want append data while doing post use angular.extend method or more simpler way would can access object using literal & set value.
code
$scope.addnew = function(product, addnewform) { angular.extend({}, product, {date_update: currentdate}); //product["date_update"] = currentdate; //this alternative $http.post('api/add', product).success(function(data){ // , i've tried add here console.log(data); $scope.reset(); $scope.activepath = $location.path('/products'); }); $scope.reset = function() { $scope.product = angular.copy($scope.master); }; $scope.reset(); };
Comments
Post a Comment