2016-02-29 47 views
0

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的默认设置;如果这有帮助的话。

编辑 这只影响复选框。其他字段会更改模型值。

+1

我的第一个想法是,这是一个范围问题。你能在这发生的地方创造一个小提琴吗? – erichardson30

+0

感谢您的帮助。我发现这不是Angular的问题,而是我的服务器端代码。答案如下。 – shteeven

回答

0

围绕以下数据后,它经过更新道琼斯模型的过程中,我发现我失踪了。它与Angular无关,而是节点中的server.controller。虽然create函数未经修改即可正常工作,但控制器中的更新函数必须更新以匹配模型。当PUT请求被发送时,当有效的ID在参数中时,中间用Dj模型填充req。

var djsPolicy = require('../policies/djs.server.policy'), 
    djs = require('../controllers/djs.server.controller'); 

module.exports = function (app) { 
    // Djs collection routes 
    app.route('/api/djs').all(djsPolicy.isAllowed) 
    .get(djs.list) 
    .post(djs.create); 

    // Single dj routes 
    app.route('/api/djs/:djId').all(djsPolicy.isAllowed) 
    .get(djs.read) 
    .put(djs.update) 
    .delete(djs.delete); 

    // Finish by binding the dj middleware 
    app.param('djId', djs.djByID); // HERE! }; 

这是然后传递到更新功能,在那里我应该在请求主体与那些在道琼斯模型具有匹配的字段。原代码:

exports.update = function (req, res) { 

    var dj = req.dj; 

    dj.title = req.body.title; 
    dj.content = req.body.content; 

    dj.save(function (err) { 
    if (err) { 
     return res.status(400).send({ 
     message: errorHandler.getErrorMessage(err) 
     }); 
    } else { 
     res.json(dj); 
    } 
    }); 
}; 

原代码也有外地的标题,这使得它看起来好像在浏览器中测试和改变这个领域在更新功能工作正常,但标志着复选框时失败。工作代码是:

exports.update = function (req, res) { 
    var dj = req.dj; 

    dj.title = req.body.title; 
    dj.image = req.body.image; 
    dj.images = req.body.images; 
    dj.links = req.body.links; 
    dj.categories = req.body.categories; 
    dj.description = req.body.description; 
    dj.guest = req.body.guest; 
    dj.featured = req.body.featured; 

    dj.save(function (err) { 
    if (err) { 
     return res.status(400).send({ 
     message: errorHandler.getErrorMessage(err) 
     }); 
    } else { 
     res.json(dj); 
    } 
    }); 
}; 
1

只是我的猜测,尝试在访问它之前初始化对象;目前还不清楚如何设置其他字段(效果),也许他们在范围上直接设置,不属于DJ命名空间

$scope.authentication = Authentication; 
$scope.dj = {}; 
. 
. 
. 
$scope.update = function (isValid) { 
    var dj = $scope.dj; 

验证,更新方法中添加一条线调试,并检查DJ目的;

$scope.update = function (isValid) { 
    debugger; // it should create a breakpoint in chrome dev tools 
    var dj = $scope.dj; 

希望这有助于

相关问题