Angularjs将数组推送到范围

时间:2016-11-24 04:33:29

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

我有问题将数组推送到范围并在此之后清空数组。

我启动函数ng-click="addInput()以向包含另一个数组$scope.info.sites = [];的数组meta_keys:$scope.tags添加新表单。

如果我再次单击ng-click="addInput(),将生成表单,但已填入数据。在每个meta_keys上表示相同的值:$ scope.tags。我想我需要在生成new之前清空数组。 或者我走错了路。谢谢你的帮助。

codepen.io

  

HTML

<div ng-app="myApp" ng-controller="myCtrl">
  {{name}}

<form>
 <div class="form-group"> 
  <label for="txtDevice" class="control-label">Domain Name:</label>
  <input type="text" class="form-control" ng-model="info.name" id="txtDevice">
 </div>
 <div class="form-group">
   <label for="txtIP" class="control-label">IP Address:</label>
   <input type="text" class="form-control" ng-model="info.ip" id="txtIP">
 </div>
 <div class="form-group">
    <label for="txtUsername" class="control-label">Logo:</label>
    <input type="text" class="form-control" ng-model="info.logo" id="txtLogo">
  </div>

<div class="form-group" ng-repeat="site in info.sites track by $index">
  <h4>Seite {{$index+1}}</h4>
<label type="txt" class="control-label">Title</label>
<input type="txt" class="form-control" ng-model="site.site_title" /><br />
<label type="txt" class="control-label">Desc</label>
<input type="text" class="form-control" ng-model="site.meta_desc"><br />
<label type="txt" class="control-label">Meta Keys</label>
<!-- <input type="text" class="form-control" ng-model="site.meta_keys"> -->
<tags-input ng-model="tags" ></tags-input>
</div>

</form>
  <button ng-click="addInput()">+add more inputs</button>
{{info | json}}
  <hr>


  tags = {{tags | json}}

</div>
  

的script.js

angular.module('myApp', ['ngTagsInput'])
    .controller('myCtrl', function($scope, $http){
      $scope.name ="myName"  ;
      $scope.info = {};
      $scope.info.sites = [];
      $scope.tags = []; 


      $scope.addInput = function() { 
        //$scope.tags = []; 
         $scope.info.sites.push({
                          site_title:'',
                          meta_desc:'',
                          meta_keys:$scope.tags})

 }
      // $scope.info.sites.meta_keys = [];

});

1 个答案:

答案 0 :(得分:0)

只需替换

<tags-input ng-model="tags" ></tags-input>

<tags-input ng-model="site.tags" ></tags-input>

我希望,这有助于:)