Html 输入文本输入后,ng-model 不再更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19310129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:21:22  来源:igfitidea点击:

ng-model no longer updates after typing into text input

javascripthtmlangularjsangular-ngmodel

提问by EvWill

I am new to AngularJS and I am having a problem that I am having trouble solving, there was a similar question on stackoverflow but it didn't seem to help me out. I basically have a form that gets updated by ng-click, but once once I enter text into any of the text boxes, those text boxes no longer update.

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

This is my HTML

这是我的 HTML

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>



This is my function that is called when someone clicks on a link



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

$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;
  })
}

回答by rickhuynh

your link requires a login.

您的链接需要登录。

if i have to guess about your problem, it may be related to angular scoping issue. try changing your ng-model binding to an object property instead. so in your html, instead of:

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

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

do this

做这个

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

and in your javascript, on the ajax callback, change it to:

并在您的 javascript 中,在 ajax 回调中,将其更改为:

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

for more info on this issue, see: https://github.com/angular/angular.js/wiki/Understanding-Scopes

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