AngularJS ng-repeat问题并在按钮单击时动态添加行

时间:2016-11-22 02:08:40

标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

我想用一些嵌套数据创建一个简单的表单。当我单击addSite()功能时,我想创建一个新的站点项并将其附加到我的$scope.info

我无法创建多个表单,并且$index变量不会传递给作用域。这是一个,

plunkr link

HTML

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body> 
  <div ng-app="myApp" ng-controller="myCtrl">

    <form>
      <label for="Domain Name">Domain Name</label>
      <input ng-model="info.name" type="text" /><br />
      <label for="IP">IP</label><input ng-model="info.ip" type="text" />
      <hr>
    <div ng-repeat="site in sites track by $index">
      <label for="">ID</label><input type="text" ng-model="site.id" />
      <label for="">title</label><input type="text" ng-model="site.title" />
      <label for="">desc</label><input type="text" ng-model="site.meta_desc" />
      <label for="">meta_keys</label><input type="text" ng-model="site.meta_keys" />


    </div>
      <br />
      <button ng-click="addSite()">Add Site</button>
    </form>

      <pre>
        {{info | json}}

      </pre>


</div>

的script.js

angular.module('myApp', [])
.controller('myCtrl', function($scope){
  $scope.info = {};
  $scope.info.sites = {};


  $scope.addSite = function(){
    $scope.info.sites = {id:'',
                         title:'',
                         meta_desc:'',
                         meta_keys:''
    }

}


});

我尝试生成的数据:

{
  "name": "myDomain",
  "ip": "11.22.33.44.55",
  "sites": {
    {"id": "1" ,"title": "myTitle Site 1","meta_desc": "myDescription Site 1", "meta_keys": ["my", "meta", "keys"]},
    {"id": "2" ,"title": "myTitle Site 2","meta_desc": "myDescription Site 2", "meta_keys": ["my", "meta", "keys"]},
    {"id": "3" ,"title": "myTitle Site 3","meta_desc": "myDescription Site 3", "meta_keys": ["my", "meta", "keys"]}
},

}

我现在得到的数据:

{
  "sites": {
    "id": "",
    "title": "",
    "meta_desc": "",
    "meta_keys": ""
  },
  "name": "myDomain",
  "ip": "11.22.33.44.55"
}

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

你做了一些错误,如下所示。

  1. 您的ng-repeat="site in sites track by $index"正在迭代 您sites
  2. 中的info.sites代替$scope.info.sites
  3. 同样$scope.info.sites也应该是array $scope.info.sites = [] 而不是object
  4. $scope.info.sites = {}
  5. 使用addSite方法而不是向数组添加新项目 $scope.info.sites.push(),你正在覆盖价值 分配它。
  6. 在纠正上述内容之后,它发挥了作用。

    angular.module('myApp', [])
        .controller('myCtrl', function($scope){
          $scope.info = {};
          $scope.info.sites = [];
          
          
          $scope.addSite = function(){
            $scope.info.sites.push({id:'',
                                 title:'',
                                 meta_desc:'',
                                 meta_keys:''
            });
      
         };
          
          
        });
    <!DOCTYPE html>
    <html>
    
      <head>
        <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script src="script.js"></script>
      </head>
    
      <body> 
      <div ng-app="myApp" ng-controller="myCtrl">
      
        <form>
          <label for="Domain Name">Domain Name</label>
          <input ng-model="info.name" type="text" /><br />
          <label for="IP">IP</label><input ng-model="info.ip" type="text" />
          <hr>
        <div ng-repeat="site in info.sites track by $index">
          <label for="">ID</label><input type="text" ng-model="info.id" />
          <label for="">title</label><input type="text" ng-model="site.title" />
          <label for="">desc</label><input type="text" ng-model="site.meta_desc" />
          <label for="">meta_keys</label><input type="text" ng-model="site.meta_keys" />
          
          
        </div>
          <br />
          <button ng-click="addSite()">Add Site</button>
        </form>
          
          <pre>
            {{info | json}}
            
          </pre>
          
          
    </div>

    方法2 :(使用Object代替数组)

    angular.module('myApp', [])
        .controller('myCtrl', function($scope){
          $scope.info = {};
          $scope.info.sites = {};
          
          
          $scope.addSite = function(){
            var index = Object.keys($scope.info.sites).length;
            $scope.info.sites['item' + index] = {id:'',
                                 title:'',
                                 meta_desc:'',
                                 meta_keys:''
            };
      
         };
          
          
        });
    <!DOCTYPE html>
    <html>
    
      <head>
        <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script src="script.js"></script>
      </head>
    
      <body> 
      <div ng-app="myApp" ng-controller="myCtrl">
      
        <form>
          <label for="Domain Name">Domain Name</label>
          <input ng-model="info.name" type="text" /><br />
          <label for="IP">IP</label><input ng-model="info.ip" type="text" />
          <hr>
        <div ng-repeat="(idx, site) in info.sites track by $index">
          <label for="">ID</label><input type="text" ng-model="info.id" />
          <label for="">title</label><input type="text" ng-model="site.title" />
          <label for="">desc</label><input type="text" ng-model="site.meta_desc" />
          <label for="">meta_keys</label><input type="text" ng-model="site.meta_keys" />
          
          
        </div>
          <br />
          <button ng-click="addSite()">Add Site</button>
        </form>
          
          <pre>
            {{info | json}}
            
          </pre>
          
          
    </div>