簡體   English   中英

ng-model無法在AngularJS中使用ng-value

[英]ng-model is not working with ng-value in AngularJS

以下是我的查看頁面

<form name="form">
     <label>Name</label>
     <input name="name" type="text" ng-model='user.name' ng-value='emp.name' required />
     <span ng-show="form.name.$touched && form.name.$invalid">Name is required</span>
     <button ng-disabled="form.name.$touched && form.name.$invalid" ng-click='formUpdate()'>Update</button>
</form>

這是我的控制器

$scope.formUpdate = function() {
  $scope.status = false;
  $http({
  method : 'POST',
  url : 'model/update.php',
  data :   $scope.user ,
  headers : {'Content-Type': 'application/x-www-form-urlencoded'}          

  }).then(function mySuccess(response) {
    $scope.update = response.data;
    console.log(response.data); 
   }, function myError(response) {
    $scope.update = response.statusText;

   }); 
};

當我在HTTP調用中使用數據: $scope.user ,我在控制台上得到的是空白值,但是如果使用數據: $scope.emp ,則將永遠不會獲得輸入字段的更新值,而不會獲得輸入字段的舊值。

ng-value將給定的表達式綁定到元素的值。

據我了解您的問題,您正在嘗試將輸入值初始化為emp.name

您應該將輸入更改為:

<input type="text" ng-model='user.name' ng-init='user.name = emp.name' required />

ng-init文檔

試試這個代碼;

$scope.formUpdate = function(){
  $scope.status = false;
  $scope.$applyAsync();

  $http({
  method : 'POST',
  url : 'model/update.php',
  data :   $scope.user ,
  headers : {'Content-Type': 'application/x-www-form-urlencoded'}          

  }).then(function mySuccess(response) {
    $scope.update = response.data;
    $scope.$applyAsync();
    console.log(response.data); 
   }, function myError(response) {
    $scope.update = response.statusText;  
    $scope.$applyAsync();
   }); 

  };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM