【发布时间】:2016-11-24 12:55:57
【问题描述】:
我无法将数组推送到作用域并在此之后清空数组。
我启动函数ng-click="addInput() 向数组$scope.info.sites = []; 添加一个新表单,其中包含另一个数组meta_keys:$scope.tags。
如果我再次单击ng-click="addInput(),将生成表单,但已经填写了数据。表示每个 meta_keys:$scope.tags 上的相同值。我想我需要在生成新数组之前清空数组。
或者我走错路了。感谢您的帮助。
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 = [];
});
【问题讨论】:
-
您的问题不是很清楚您在寻找什么,您能否详细说明一下并将您的代码与您的问题相关联。我认为,您的代码和问题不同步且令人困惑。
标签: angularjs angularjs-scope angularjs-ng-repeat