Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJs ng submit无法获取ng模型的值_Angularjs_Input_Angular Ngmodel_Ng Submit - Fatal编程技术网

AngularJs ng submit无法获取ng模型的值

AngularJs ng submit无法获取ng模型的值,angularjs,input,angular-ngmodel,ng-submit,Angularjs,Input,Angular Ngmodel,Ng Submit,我有这个HTML: <form ng-submit="addComment()"> <md-input-container flex> <label>Shoutout</label> <input type="text" ng-model="comment"> </md-input-container> </form> $scope.comment = "";

我有这个HTML

<form ng-submit="addComment()">
    <md-input-container flex>
        <label>Shoutout</label>
        <input type="text" ng-model="comment">
    </md-input-container>
</form>
$scope.comment = "";

$scope.addComment = function(){
    console.log("Value from input", $scope.comment);
    $scope.comment = "test"
    console.log("New Value", $scope.comment);
}
当im在
输入上键入模型正在更新和所有内容时,它工作正常。但当我按下enter键时,我希望输入的值会记录在控制台上。但它似乎无法从
ng模型
中获取更新值


尝试将其作为参数传递:

<form ng-submit="addComment(comment)">
  <md-input-container flex>
    <label>Shoutout</label>
    <input type="text" ng-model="comment">
  </md-input-container>
</form>

$scope.addComment = function(comment){
  console.log("Value from input", comment);
  $scope.comment = "test";
  console.log("New Value", comment);
}

叫喊
$scope.addComment=函数(注释){
日志(“输入值”,注释);
$scope.comment=“测试”;
日志(“新值”,注释);
}

看一看,似乎都是正确的

angular.module('app',[]).controller('MyController',['$scope',
职能($范围){
$scope.comment=“”;
$scope.addComment=函数(){
log(“输入值”,$scope.comment);
$scope.comment=“测试”
log(“新值”,$scope.comment);
}
}
]);

叫喊
{{“日志-->>”+注释}


很好,谢谢。不过有一个问题。如何使
输入再次为空?@GreenFox Just do comment=“”;我在
addComment()
中执行
$scope.comment=”“
,但它不起作用:(如果您将其设置为null会怎样?问题似乎是因为DOM在Javascript之前加载,因为我基于http的响应通过ng repeat填充视图。我通过在输入上放置一个动态id来解决问题,并使用jquery清空输入,如
$('#comment.'+someid).val('');