I have a form with drop downs like this:
<select id="relevance" class="form-control input-lg" ng-model="formData.relevance"></select>
I can fill all these dropdowns using jquery that maps the json data to the appropriate dropdown like this:
$.getJSON("url/to/the/json", function(data) {
// get values from database
var rel_val = data[0].relevance;
// set fields using jquery
$("#relevance").val(rel_val);
This all works great, but if I click on the form submit button:
<button type="submit" id="submit_button" class="btn btn-primary btn-lg btn-block" ng-click="createTodo();">SAVE CHANGES</button>
It will not write anything to the database (postgres). I know that the "createTodo()" works because if I manually make changes to the dropdowns (so set by me, not jquery) the form will submit the data properly.
My "createTodo" looks like this:
// Create a new todo
$scope.createTodo = function(todoID) {
$http.post('/api/v1/todos', $scope.formData)
.success(function(data) {
$scope.formData = {};
$scope.todoData = data;
console.log(data);
})
.error(function(error) {
console.log('Error: ' + error);
});
};
So, how can I still submit my form data using values set by jquery instead of manually? Does it have something to do with binding?
I have tried adding this to my controller:
$http.get('/api/v1/todos')
.success(function(data) {
$scope.formData.relevance = data[0].relevance;
})
where my ng-model is ng-model = "formData.relevance" and my value of relevance comes from data[0].relevance.