ng-model在AngularJS中不使用ng-value

时间:2017-03-22 13:09:43

标签: javascript angularjs input angularjs-1.6

以下是我的查看页面

<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,那么我永远不会获得输入字段的更新值而是获取旧值输入字段。

2 个答案:

答案 0 :(得分:2)

ng-value将给定表达式绑定到元素的值。

据我了解您的问题,您正尝试将输入值初始化为 [HttpGet] public ActionResult WelcomeScreen() { // Формируем список команд для передачи в представление SelectList teams = new SelectList(db.Vacancy, "VacancyId", "VacancyName"); ViewBag.Teams = teams; SelectList teams2 = new SelectList(db.Companies, "CompanyID", "CompanyName"); ViewBag.Teams2 = teams2; return View(); } [HttpPost] public ActionResult WelcomeScreen( Interview interview) { db.Interview.Add(interview); db.SaveChanges(); return RedirectToAction("Index","Questions"); }

您应该将输入更改为:

emp.name

ng-init docs

答案 1 :(得分:0)

试试这段代码;

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

  };