简体   繁体   English

角度textarea验证错误

[英]Angular textarea validation error

I have a textarea in my app like so: 我的应用程序中有一个textarea,如下所示:

<textarea type="text" class="form-control" name="bioBackground" ng-model="obj.background" value="obj.background" ng-change="autosave()" maxlength="1500" required></textarea>

This is a required field and as you can see I have added a required tag in the code. 这是必填字段,您可以看到我在代码中添加了必需的标记。 But this required tag is causing errors with Angular. 但是这个必需的标签会导致Angular出错。

For example: 例如:

If I type in the field and backspace and remove all the text it removes the object from the scope. 如果我键入字段和退格并删除所有文本,它会从范围中删除对象。 see example below: 见下面的例子:

Before Backspacing 在退格之前

You can see background variable is in the scope. 您可以看到后台变量在范围内。

在此输入图像描述

After backspacing and removing the text: 退回并删除文本后:

You can see the background variable is gone from scope. 您可以看到背景变量已从范围中消失。

在此输入图像描述

This only happens if I use required tag. 只有在使用必需标签时才会发生这种情况 If I don't use the required tag it works fine and even after removing all the text the background variable stays within the scope. 如果我不使用所需的标签,它工作正常,甚至在删除所有文本后,后台变量仍然在范围内。

What am I doing wrong. 我究竟做错了什么。

its working , background is undefined while remove all text using backspace. 它的工作,背景是未定义的,同时使用退格删除所有文本。

 var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.name = 'World'; $scope.obj = { "experience":{ "field0":"asdf", "field1":"asss" }, "background":"" }; console.log($scope.obj); $scope.autosave=function(){ console.log($scope.obj); } }); 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>AngularJS Plunker</title> <script>document.write('<base href="' + document.location + '" />');</script> <link href="style.css" rel="stylesheet" /> <script data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js" data-require="angular.js@1.4.x"></script> <script src="app.js"></script> </head> <body ng-app="plunker" ng-controller="MainCtrl"> <textarea type="text" class="form-control" name="bioBackground" ng-model="obj.background" value="obj.background" ng-change="autosave()" maxlength="1500" required></textarea> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM