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