I am able to get both attribute name and value from user input
{{attribute}} and {{value}}
how do i add these to an existing json object
$scope.items ={
name: peter,
age: 16,
{{attribute}}: {{value}}
}
You could use bracket notation.
$scope.items[$scope.attribute] = $scope.value;
Here's the plunker for your problem statement,using ng-change I've called a function to create the JSON object dynamically when ever user changes anything in the input fields or form.
Below is the look of the controller where we create the JSON object
var myApp = angular.module('Test', []);
myApp.controller('MainCtrl', ['$scope', function($scope) {
$scope.input={};
$scope.items={};
$scope.createObj = function(){
$scope.items['name'] = $scope.input.name;
$scope.items['age'] = $scope.input.age;
};
}]);
HTML will look like this:
<body ng-app="Test" ng-controller="MainCtrl">
<h5>Name:</h5>
<input type="text" ng-model="input.name" ng-change="createObj()"/>
<h5>Age :</h5>
<input type="text" ng-model="input.age" ng-change="createObj()"/>
<pre>{{items}}</pre>
</body>
$scope.items[$scope.attribute] = $scope.valuebut why you would want to do this I don't understand.