UPDATE
After further investigation, I discovered that the issue was not related to Angular itself, but rather a mistake in the update function within the node server controller. I have provided the fix below for reference, and decided to keep this question here in case it can assist others who may encounter a similar error.
ORIGINAL QUESTION
I have noticed that the Angular model does not reflect changes when a property is altered within a form. Below is the code snippet:
<section class="container" ng-controller="DjsController" ng-init="findOne()">
<form name="djForm" class="form-horizontal" ng-submit="update(djForm.$valid)" novalidate>
<fieldset>
<div>.... other form fields </div>
<div class="form-group">
<label>Guest:</label>
<input name="guest" type="checkbox" ng-model="dj.guest">
</div>
<div class="form-group">
<label>Featured:</label>
<input name="featured" type="checkbox" ng-model="dj.featured">
</div>
<button type="button" ng-click="logDj()">Log it</button>
<div class="form-group">
<input type="submit" class="btn btn-default">
</div>
</fieldset>
</form>
Upon selecting the checkbox and submitting the form, the original model gets sent to the server without being updated. To troubleshoot, I added ng-click="logDj()
to observe the model behavior. Interestingly, the model updates upon clicking the button. I am seeking a detailed explanation for this phenomenon.
Below is the included controller code:
angular.module('djs').controller('DjsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Djs',
function ($scope, $stateParams, $location, Authentication, Djs) {
$scope.authentication = Authentication;
// Clear forms
$scope.clear = ...
// Create new Dj
// $scope.create = ...
// Remove existing Dj
// $scope.remove = ...
// Update existing Dj
$scope.update = function (isValid) {
$scope.error = null;
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'djForm');
return false;
}
// shows original model if logDj() is not fired
console.log($scope.dj);
var dj = $scope.dj;
dj.$update(function () {
$location.path('djs/' + dj._id);
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};
// Find a list of Djs
//$scope.find = ....
// Find existing Dj
$scope.findOne = function () {
$scope.dj = Djs.get({
djId: $stateParams.djId
});
};
$scope.logDj = function() {
console.log($scope.dj);
};
}
]);
Initially, I suspected that the issue might stem from the property not existing beforehand, but even when the property is pre-populated during retrieval, the model remains unchanged.
This peculiar behavior only seems to affect checkboxes; the values of other fields get updated as expected.
I am using the default MEAN.JS set-up generated by Yeoman, in case that information is relevant.
EDIT I have observed that this issue specifically pertains to checkboxes, while other field values are successfully updated.