不从ng提交的值为textarea提交

时间:2016-03-18 18:29:15

标签: javascript angularjs forms ng-submit

我正在尝试模拟评论系统。如何将传递值提交给控制器?

在我的控制器里,我有

$scope.reviews = function() {
  $scope.rating = 0;
  $scope.max = 5;
};
$scope.myTextArea = '';
$scope.saveReview = function(rating, myTextArea) {
  console.log(rating);
  console.log(myTextArea);
};

在我看来,我有:

<form name="reviewForm" class="form-horizontal" ng-submit="saveReview(rating,    myTextArea)" novalidate>
  <div>
    <rating ng-model="rating" max="max" aria-labelledby="'product.title'"></rating>
  </div>
  <div>This is the rating: {{rating}}</div>
  <div class="animate-switch" ng-switch-when=true>
    <textarea ng-model="myTextArea" class="form-control" placeholder="Write a short review of the product." title="Review"></textarea>
  </div>
  <button type="submit" class="btn btn-primary pull-right">Submit Review</button>
</form>

当我提交表单时,将调用saveReview函数并在控制台中打印输出为0和''。因此,没有值被保存/传递。 Ng模型评级显示5颗星,如果您点击4颗星,{{rating}}将显示4颗。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

不要将任何内容传递到saveReview()上的ng-submit函数中。而是让saveReview()使用范围中的变量。还将变量封装在对象中。例如:ng-model="review.rating"ng-model="review.text"。假设有很多,你需要一个成为一个对象;)

$scope.saveReview = function() {
  console.log($scope.review.rating);
  console.log($scope.review.text);
};
相关问题