2016-12-25 52 views
0

我的HTML如何从ngRepeat中的文本输入中获取项目编号?

<body ng-app="myApp" ng-controller="myCTRL"> 
    <form class="my-form" ng-repeat="item in items" novalidate> 
    <h4>{{item.number}}</h4> 
    <input type="text" ng-model="item.hed"><br> 
    <input type="text" ng-model="item.subhed"> 
    </form> 
</body> 

我的JS:

var app= angular.module("myApp",[]); 
app.controller("myCTRL",function($scope,$http){ 
    $scope.items= [ 
    { 
     number: chartNumber, 
     hed: '', 
     subhed: '' 
    } 
    ]; 
}); 

当我输入一些东西到input标签,我想console.log()item.number值。我如何使用AngularJS v1.6.1做到这一点?

+0

*当我输入一些东西到'input'标签,我想'的console.log()''的值item.number' *什么? ? –

回答

1

使用NG-改变指令检测输入值的任何变化

<body ng-app="myApp" ng-controller="myCTRL"> 
     <form class="my-form" ng-repeat="item in items" novalidate> 
     <h4>{{item.number}}</h4> 
     <input type="text" ng-model="item.hed" ng-change="inputChange(item)"><br> 
     <input type="text" ng-model="item.subhed" ng-change="inputChange(item)"> 
     </form> 
    </body> 

J小号

var app= angular.module("myApp",[]); 
app.controller("myCTRL",function($scope,$http){ 
    $scope.items= [ 
    { 
     number: chartNumber, 
     hed: '', 
     subhed: '' 
    } 
    ]; 
    $scope.inputChange = function(item) { 
    console.log(item.number); 
    } 
}); 
1

希望这有助于:

您的看法:

<input type="text" ng-model="item.hed" ng-keyup="$parent.inputHandler(item)"> 

控制器:

app.controller("myCTRL",function($scope,$http){ 
    $scope.items= [ 
    { 
     number: chartNumber, 
     hed: '', 
     subhed: '' 
    } 
    ]; 

    $scope.inputHandler = function(item){ 
     console.log(item.number); 
    } 

}); 
1

angular.module("myApp",[]) 
 
.controller("myCTRL",function($scope){ 
 
    $scope.items= [ 
 
    { 
 
     number: 1, 
 
     hed: '', 
 
     subhed: '' 
 
    }, 
 
    { 
 
     number: 2, 
 
     hed: '', 
 
     subhed: '' 
 
    } 
 
    ]; 
 
    $scope.log = function(num) { 
 
    console.log(num); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCTRL"> 
 
    <form class="my-form" ng-repeat="item in items" novalidate> 
 
    <h4>{{item.number}}</h4> 
 
    <input type="text" ng-model="item.hed", ng-keypress="log(item.number)"><br> 
 
    <input type="text" ng-model="item.subhed", ng-keypress="log(item.number)"> 
 
    </form> 
 
</div>

相关问题