angularjs获取拼接字符串的值

在前台做了一个拼接的ng-model,想获取ng-model的值,有什么方法吗?
<input ng-model = 'name_1' value = '1'>
<input ng-model = 'name_2' value = '2'>

/js代码*/
for(var i = 0;i<3;i++){

//如何通过for循环获取name_1和Name_2的值?

}

阅读 6.1k
1 个回答

效果图:
clipboard.png
代码(可直接运行查看效果):

<!DOCTYPE html>
<html ng-app="myApp">
<head>
  <meta charset="utf-8">
  <title>angularJs</title>
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  <style>
  body{font-size:14px;line-height:1.42857143;color: #000;}
  .container{margin-top:100px;}
  </style>
  <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div class="container" ng-controller="myCtrl as vm">
  <form name="resumeNameForm" novalidate class="form-horizontal" method="post" autocomplete="off">
    <div class="row form-group" ng-repeat="languageItem in vm.languageInfo.operate">
      <label class="col-md-2 control-label"><span ng-show="!$index">语言要求:</span></label>
      <div class="col-md-5">
        <input class="form-control" ng-model="languageItem.value">
      </div>
      <div class="col-md-5">
        <button class="btn btn-sm btn-default" type="button" ng-disabled="vm.languageInfo.operate.length >= 4" ng-click="vm.languageInfo.add($index)">+</button>
        <button class="btn btn-sm btn-default" type="button" ng-click="vm.languageInfo.delete($index)" ng-show="$index">-</button>
      </div>
    </div>
    <div class="row form-group">
      <div class="col-md-6 col-md-offset-2">
        <input class="btn btn-primary" type="submit" ng-click="vm.save()" value="确认">
      </div>
    </div>
    <div class="row form-group">
      <div class="col-md-6 col-md-offset-2">{{ vm.jobInfoData}}</div>
    </div>
  </form>
</div>
<script>
  angular.module('myApp', [])
    .controller('myCtrl', function($scope) {
      var vm = this;
      vm.languageInfo = {};
      vm.languageInfo.operate = [];
      // 新增语言要求
      vm.languageInfo.add = function($index) {
        vm.languageInfo.operate.splice($index + 1, 0, {
          key: $index + 1,
          value: ''
        });
      };
      // 删除语言要求
      vm.languageInfo.delete = function($index) {
        vm.languageInfo.operate.splice($index, 1);
      };

      vm.save=save;
      activate();

      ////////////////////////////////

      function activate() {
        //初始化职位语言要求
        vm.languageInfo.operate.push({
          key: 0,
          value: ''
        });
      }

      function save(argument) {
        vm.jobInfoData = {
          languageTypes: []
        };
        //语言要求
        angular.forEach(vm.languageInfo.operate, function(data) {
          if (data.value) {
            vm.jobInfoData.languageTypes.push(data.value);
          }
        });
      }
    });

</script>

</body>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进