2013-10-11 40 views
29

我是AngularJS的新手,我遇到了一個問題,我遇到了問題,在stackoverflow上有類似的問題,但它似乎沒有幫助我。我基本上有一個通過ng-click進行更新的表單,但是一旦我將文本輸入到任何文本框中,這些文本框就不再更新。ng-model在鍵入文本輸入後不再更新

這是我的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> 



這是我的函數被調用,當有人點擊一個鏈接

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

回答

115

你的鏈接需要登錄。

如果我必須猜測您的問題,它可能與角度範圍問題有關。嘗試更改您的ng模型綁定到對象屬性。所以在你的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

+5

非常感謝你,它在約60秒內修好了它。在繼續這個項目之前,我一定會詳細閱讀範圍,因爲我顯然不完全理解它們。 – EvWill

+3

@EvWill很好的解釋,爲什麼它不與我使用原始這裏https://github.com/angular/angular.js/wiki/Understanding-Scopes – Mirko

+3

它錯誤的方式進行一個月以上的工作,非常感謝你。如果這不是正確的方式,那麼Angular不應該允許開發人員以這種方式編寫代碼。這個問題讓我震驚了整整兩個晚上。 :D –