如何在angularjs中获取inputbox的值(索引)

时间:2016-10-12 06:50:48

标签: angularjs angularjs-directive angularjs-scope

Angularjs

<tr ng-repeat="advLi in media">
    <td>
        <input style="display:none;" type="text" style="width:35px;" class="form-control" name="mediaindex" value="{{$index+1}}" ng-model="advLi.media_order">
    </td>
    <td>{{advLi.media_name}}</td>
</tr>

当我提交表格时,&#34;媒体&#34;列表未显示media_order,仅显示media_name。如何在控制器中获取media_order值。

2 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您希望在文本框中将项目索引值显示为序列号,用户可以稍后更改它以重新排列顺序。

然后我在输入标记中添加了ng-repeat,以便在ng-model中创建新的DOM元素时,我们会使用索引+ 1初始化var app = angular.module('app', []); app.controller('MainCtrl', function($scope) { $scope.media =[{media_name:"Still"},{media_name:"Gangsta"},{media_name:"Weed it out"}]; $scope.submit = function (){ angular.forEach($scope.media,function(val,key){ console.log(val.media_order + ' - '+ val.media_name); }); }; });值,这将显示文本框上的序列号。我还删除了隐藏文本框的样式。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="app" ng-controller="MainCtrl">
  <table> 
    <tr ng-repeat="advLi in media">
    <td>
        <input  type="text" style="width:35px;" class="form-control" name="mediaindex" value="{{$index+1}}" ng-init="advLi.media_order = $index + 1" ng-model="advLi.media_order">
    </td>
      <td>{{$index +1}} </td>
    <td>{{advLi.media_name}}</td>
      <td>{{advLi.media_order}}</td>
</tr>
    </table>
  <button ng-click="submit()">Submit </button>
  </body>
Console.WriteLine(DateTime.Now.ToUniversalTime() - DateTime.UtcNow.ToUniversalTime());
Console.WriteLine(DateTime.Now.ToUniversalTime() - DateTime.UtcNow);
Console.WriteLine(DateTime.Now - DateTime.UtcNow);

答案 1 :(得分:0)

可能由undefined初始值ng-model引起。尝试交换属性顺序,或设置正确的ng-model