2016-05-05 5 views
0

私にはデータを追加するためのフォームがあります。データ更新の場合も同じです。私は、$ http post経由で特定のIDに対してデータを取得することによってフォームにデータを埋め込みます。私はスコープ変数を変更しましたが、変更された値を取得しませんでした。ここに私の努力です:

<input type="hidden" name="form_type" ng-model="formData.formType" ng-init="formData.formType = submit_button" value="{{submit_button}}"/> 

<input type="hidden" name="student_id" ng-model="formData.studentId" ng-init="formData.studentId = student_id" value="{{student_id}}"/> 

$scope.Edit = function (id) { 
     $http({ 
       method: 'POST', 
       url: 'process.php', 
       data: $.param({action: 'fetch', id: id}), 
       headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
      }).success(function (response) { 
       if (!response.success) { 
        alert("something went wronge"); 
        return false; 
       } else { 
        $scope.formData = response.data; 
        $scope.student_id = response.data.id; 
        $scope.submit_button = 'Update'; 
        $scope.$apply(); 
       } 
      }); 
    }; 

enter image description here

+2

この記事のトップの答えは役に立つかもしれ:http://stackoverflow.com/questions/22733422/angularjs-rootscopeinprog-inprogress-error – user95227

+1

しかし、なぜあなたは 'を呼び出す必要があります$ scope。$ apply() '?ダイジェストサイクルが自動的に開始されますが、なぜ手動で開始するのですか? – iulian

+0

@iulian隠しフィールドのスコープ変数を変更した後に、ポストデータの値が更新されないためです。 –

答えて

0

これは安全な範囲に適用する方法です。

$scope.safeApply = function(fn) { 
 
    var phase = this.$root.$$phase; 
 
    if(phase == '$apply' || phase == '$digest') { 
 
    if(fn && (typeof(fn) === 'function')) { 
 
     fn(); 
 
    } 
 
    } else { 
 
    this.$apply(fn); 
 
    } 
 
}; 
 

 

 
$scope.safeApply(function() { 
 
    alert('Now I'm wrapped for protection!'); 
 
});

関連する問題