NG-model 在输入文本后不再更新

ng-model no longer updates after typing into text input

本文关键字:不再 更新 文本 输入 NG-model      更新时间:2023-09-26

我是 AngularJS 的新手,我遇到了一个无法解决的问题,在 stackoverflow 上有一个类似的问题,但它似乎没有帮助我。我基本上有一个通过ng-click更新的表单,但是一旦我在任何文本框中输入文本,这些文本框就不再更新。

这是我的网页

Edit Course:
<li ng-repeat="course in courses">
  <p>
    <a ng-click="Edit_Course(course.id)">{{course.course_name}}</a>
  </p>
</li>
<div ng-show="showedit == 1">
  <form novalidate ng-submit="edit_course()" class="simple-form">
    <label for="form_course_name">Course</label>
      <input type="text" id="form_course_name" ng-model="edit_course_name">
    <label for="form_par">Par</label>
      <input type="text" id="form_par" ng-model="edit_course_par">
    <label for="form_course_location">Course Location</label>
      <input type="text" id="form_course_location" ng-model="edit_course_location">
     <input type="submit" id="submit" value="Edit Course" />
   </form>
</div>



这是我的函数,当有人点击链接时调用

$scope.Edit_Course = function (id) {
    var course = {
        'course_id' : id
    };
    $http({method: "POST", url: "http://www.dgcharts.com/editcourse", data: course})
    .success(function(data, status, headers, config){
      thecourse = data["course"];
      $scope.edit_course_name = thecourse.course_name;
      $scope.edit_course_par = thecourse.par;
      $scope.edit_course_location = thecourse.course_location;
      $scope.edit_course_id = thecourse.id;
      $scope.showedit = 1;
  })
}

您的链接需要登录。

如果我必须猜测您的问题,它可能与角度范围问题有关。 请尝试改为将 ng-model 绑定更改为对象属性。 所以在你的 HTML 中,而不是:

<input type="text" id="form_course_name" ng-model="edit_course_name">

这样做

<input type="text" id="form_course_name" ng-model="course.edit_course_name">

在你的JavaScript中,在Ajax回调上,将其更改为:

$scope.course = {};  //only do this if $scope.course has not already been declared
$scope.course.edit_course_name = thecourse.course_name;

有关此问题的详细信息,请参阅:https://github.com/angular/angular.js/wiki/Understanding-Scopes