加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何清除动态ng-repeat表单字段

发布时间:2020-12-17 10:22:48 所属栏目:安全 来源:网络整理
导读:问题:如何清除动态创建的ng-repeat AngularJS表单字段?如果你能找到一个我没有找到答案的地方,我会感到惊讶. 背景:我有AngularJS通过服务将JSON拉入我的控制器.然后我使用范围来重复表格的重复标签.我无法清理田地.因为单词没有准确地告诉你我在做什么是
问题:如何清除动态创建的ng-repeat AngularJS表单字段?如果你能找到一个我没有找到答案的地方,我会感到惊讶.

背景:我有AngularJS通过服务将JSON拉入我的控制器.然后我使用范围来重复表格的重复标签.我无法清理田地.因为单词没有准确地告诉你我在做什么是基本的代码设置.我把它砍成了几行.

我试过旧的$scope.formName.inputName =“”;和$scope.inputName =“”;,但它们不起作用.任何想法或方向去?

http://plnkr.co/edit/BtID7a8EnyxuxClwdHkS?p=preview

<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
<link href="style.css" rel="stylesheet" />
<script src="app.js"></script>
</head>
<body ng-app="app" ng-controller="AppTest as app">
    <form name="formName" id="formName" style="width: 320px">
        <div ng-repeat="item in currentInfo.attribute">
            <div style="float:left;">{{item.desc}} </div>
            <div style="float:left;">
                <input name="forminput" ng-model="forminput" style="width:200px" type="text" value=""/>
            </div>
        </div>
        <button value="Clear" style="float:left;" ng-click="clearMe()">Clear</button>
    </form>
</body>
</html>

var app = angular.module("app",[]);
app.controller("AppTest",function($scope) {
$scope.currentInfo = {
"attribute": [
    {
        "name": "ACCT","desc": "Account #",},{
        "name": "FNAME","desc": "First Name","type": "VARCHAR","validation": "^[a-zA-Zs]+"
    },{
        "name": "LNAME","desc": "Last Name",{
        "name": "MNAME","desc": "Middle Name","type": "CHAR","validation": "^[a-zA-Z]+[1-9]+"
    }
]
};
$scope.clearMe = function (){
    $scope.forminput = "";
};
});
您正在重复使用每个唯一的ngmodel =“forminput”使用,方法是输入一个对象并使用键创建唯一的模型ng-model =“forminput [item.desc]”

首先在您的控制器中

$scope.forminput = {};

然后在视图中,将输入更改为

演示:

// Code goes here

var app = angular.module("app",[]);

app.controller("AppTest",function($scope) {
   $scope.forminput = {};
  $scope.currentInfo = {
    "attribute": [
        {
            "name": "ACCT",{
            "name": "FNAME","validation": "^[a-zA-Zs]+"
        },{
            "name": "LNAME",{
            "name": "MNAME","validation": "^[a-zA-Z]+[1-9]+"
        }
    ]
  };
  $scope.clearMe = function (){
    console.log("herleme")
    $scope.forminput = {};
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="AppTest as app">
    <h1>Hello Plunker!</h1>
    <form name="formName" id="formName">
      <div ng-repeat="item in currentInfo.attribute">
          <div style="float:left;">{{item.desc}} </div>
				<div > <input name="forminput[item.desc]" ng-model="forminput[item.desc]" style="width:200px" type="text" value=""/>
      </div>
      </div>
      <button value="Clear" ng-click="clearMe()">Clear</button>
    </form>
      
  </body>
<input name="forminput[item.desc]" 
ng-model="forminput[item.desc]" 
style="width:200px" type="text" value=""/>

并清除它

$scope.clearMe = function (){
    console.log("herleme")
    $scope.forminput = {};
  };

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读