Angularjs 将数组推送到作用域

问题描述 投票:0回答:1

我在尝试将数组推入范围并随后清空数组时遇到问题。

我启动函数

ng-click="addInput()
将新表单添加到数组
$scope.info.sites = [];
,其中包含另一个数组
meta_keys:$scope.tags

如果我再次单击

ng-click="addInput()
,表单将生成,但已填写数据。意味着每个 meta_keys:$scope.tags 上的值相同。我想我需要在生成新数组之前清空数组。 或者我走错路了。感谢您的帮助。

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>

脚本.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 = [];

});
angularjs angularjs-scope angularjs-ng-repeat
1个回答
0
投票

更换线即可

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

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

我希望,它有帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.