I am trying to build a form with Angular that creates inputs based off fields in a mongoDB using ng-repeat. Im new to angular so I am not quite sure how to execute this properly.
Here is simplified html:
<form ng-controller="SettingsFormCtrl as form" ng-submit="form.processForm()" novalidate>
<div class="tabs">
<div class="usertab" ng-repeat="(field,value) in formData.usertab">
<input ng-model="{{field}}" name="{{field}}" value="{{value}}" required>
<input type="submit">
</div>
<div class="infotab" ng-repeat="(field, value) in formData.infotab">
<input ng-model="{{field}}" name="{{field}}" value="{{value}}" required>
<input type="submit">
</div>
</div>
</form>
Here is app.js:
function SettingsFormCtrl($scope,$http){
var profile = this;
$http.get("api/profile/").success(function(response) {
profile.result = response;
});
$scope.formData = profile.result;
$scope.processForm = function() {
$http.post('pi/profile/submit', $scope.formData) .success(function(data) {
console.log(data);
if (!data.success) {
// if not successful, bind errors to error variables
alert('succes!');
} else {
// if successful, bind success message to message
alert('no');
}
});
};
}
angular
.module('inspinia')
.controller('ProfileCtrl',ProfileCtrl)
.controller('SettingsFormCtrl',SettingsFormCtrl)
And here is the .get Data:
{
"usertab":
{
"username":"Bob",
"email":"[email protected]"
},
"infotab":
{
"phone":"988-333-1245",
"age":"44"
}
}
Any help is definitely appreciated.
json_encode()to match with MongoDB way of storing things as objects