最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs ng-model not working input[text]) - Stack Overflow

programmeradmin0浏览0评论

I have a data from the database in a loop and this works great, and the value is in input field, but when i want read this value after click angular return me:

Object {newName: undefined, newEmail: undefined}

and i don't know why. Please for help.

HTML CODE:

<tr ng-repeat="user in users">
    <td><input type="text" ng-model="user.name" name="user.name" ></td>
    <td><input type="text" ng-model="user.email" name="user.email" ></td>
    <td><button ng-click="checkData()">Click</button></td>
</tr>

CONTROLLER CODE:

 $scope.user = {};
 $scope.checkData = function () {
   var data = {
      newName: $scope.user.name,
      newEmail: $scope.user.email
   };
 console.log(data);

I have a data from the database in a loop and this works great, and the value is in input field, but when i want read this value after click angular return me:

Object {newName: undefined, newEmail: undefined}

and i don't know why. Please for help.

HTML CODE:

<tr ng-repeat="user in users">
    <td><input type="text" ng-model="user.name" name="user.name" ></td>
    <td><input type="text" ng-model="user.email" name="user.email" ></td>
    <td><button ng-click="checkData()">Click</button></td>
</tr>

CONTROLLER CODE:

 $scope.user = {};
 $scope.checkData = function () {
   var data = {
      newName: $scope.user.name,
      newEmail: $scope.user.email
   };
 console.log(data);
Share Improve this question edited Oct 21, 2015 at 15:17 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Oct 21, 2015 at 14:55 KakerKaker 6451 gold badge11 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Inside your ng-repeat code you have displayed each element of users & you are showing the one below another inside tr of table. While you wanted to access the element which you are editing, You need to pass that user object to the checkData method so that you can have access to that specific specific user.

Markup

<tr ng-repeat="user in users">
    <td><input type="text" ng-model="user.name" name="user.name" ></td>
    <td><input type="text" ng-model="user.email" name="user.email" ></td>
    <td><button ng-click="checkData(user)">Click</button></td>
</tr>

Code

$scope.checkData = function (user) {
   var data = {
      newName: user.name,
      newEmail: user.email
   }
   console.log(data);
};

In your ng-click pass the user model such that it bees ng-click="checkData(user)"

Then, rewrite your controller code as:

$scope.user = {};
$scope.checkData = function (user) {
  console.log(user);
};

You need to give the function the data from the DOM you want to evaluate, which is the current user:

You can use

<tr ng-repeat="user in users">
  <td>{{user.name}}</td>
  <td>{{user.email}}</td>
  <td><button ng-click="check(user)"></button></td>
</tr>



$scope.check = function (thing) {
  var currentUser = {
    name: thing.name,
    email: thing.email
  }
}
发布评论

评论列表(0)

  1. 暂无评论