UPDATE角模型只有一个的console.log()
我发现,这个问题是不是有角,但在节点服务器控制器的更新功能的错误之后更新。解决办法在下面,我在这里留下问题来帮助那些可能犯过同样错误的人。当属性在形式改变
原来的问题
角模式不会改变。代码:
<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>
当我选中该复选框,或者将真或假,并提交表单,原始模型被发送到服务器,而不是更新。然后我插入ng-click="logDj()
来记录模型,看看发生了什么。但是,当我点击它时,模型会更新。我在寻找的是为什么这是更详细的解释?
这里是控制器:
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);
};
}
]);
我想也许是因为财产不曾存在的,它可能会导致这种现象,但填充的财产,即使检索时,模型拒绝改变。
我使用Yeoman的MEAN.JS的默认设置;如果这有帮助的话。
编辑 这只影响复选框。其他字段会更改模型值。
我的第一个想法是,这是一个范围问题。你能在这发生的地方创造一个小提琴吗? – erichardson30
感谢您的帮助。我发现这不是Angular的问题,而是我的服务器端代码。答案如下。 – shteeven