ng-model不从输入字段更新$ scope对象

时间:2017-07-31 13:12:30

标签: javascript angularjs forms angularjs-scope angularjs-ng-model

我有一个用于在论坛中更新答案的编辑表单:

<div ng-repeat="answer in question.answers">
    ...
    <div ng-show="answer.editMode" class="card pam">
        <edit-answer-form
            answer="answer"
        ></edit-answer-form>
   </div>
</div>

编辑答案表格如下:

{{answer.body}} <!--This appears on screen fine-->
<form ng-submit=editAnswer() method="POST">
    <input type="text" ng-model="answer.body" />
    <button type="submit">Edit</button>
</form>

指令的Javascript是:

app.directive('editAnswerForm', function() {

var editAnswerController = ['$scope', 'flashMessageService', 'answerService', '$log', function($scope, flashMessageService, answerService, $log) {

    var logger = $log.getInstance('editAnswerFormDirective');

    $scope.editAnswer = function() {
        logger.info("Edited answer to send to service ", $scope.answer);
        answerService.editAnswer($scope.answer).then(function success(response) {
            flashMessageService.flashMessageSuccess("Your answer has been edited");
        }, function error(response) {
            flashMessageService.flashMessageError("Error editing answer");
            logger.error("Error editing answer: ", response);
        })
    };
}];

return {
    // restrict: 'E',
    templateUrl: '/angular/app/answer/editAnswer.html',
    controller: editAnswerController,
    scope: {
        answer: '='
    }
}
});

$scope.answer正在被传递给指令(在屏幕上放置{{answer.xxx}}时,答案将出现在屏幕上)。但是在表单中,answer.body值不存在,在写入输入字段时也不更新模型,或在提交表单时注销值。

我有类似的指令/表格来更新问题,而且工作正常。唯一的区别是答案在ng-repeat。可能与此有关吗?

为什么这不起作用?

非常感谢。

0 个答案:

没有答案