Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 这是虫子吗?Angular在相应的输入无效时删除对象参数_Angularjs - Fatal编程技术网

Angularjs 这是虫子吗?Angular在相应的输入无效时删除对象参数

Angularjs 这是虫子吗?Angular在相应的输入无效时删除对象参数,angularjs,Angularjs,请看这个: {{contacts}json} var app=angular.module('testApp',[]); app.controller('testCtlr',[“$scope”,函数($scope){ $scope.contacts=[ { id:“”, 名称:“”, 电子邮件:[ { 电邮:'e1', 文本:“fghfgh” }, { 电邮:'e2', 文字:“hjkhjk” } ], } ]; }]); 您可以看到,如果更改电子邮件文本框中的文本,它将从obj中删除,除

请看这个:


{{contacts}json}
var app=angular.module('testApp',[]);
app.controller('testCtlr',[“$scope”,函数($scope){
$scope.contacts=[
{
id:“”,
名称:“”,
电子邮件:[
{
电邮:'e1',
文本:“fghfgh”
},
{
电邮:'e2',
文字:“hjkhjk”
}
],
}
];
}]);

您可以看到,如果更改电子邮件文本框中的文本,它将从obj中删除,除非它是有效的电子邮件…

它不是错误,它所需的行为是角度,当值无效时,它不会绑定到ngModelController

但也有可能附加自定义电子邮件验证器($parsers) $parsers


您可以将解析器推送到解析器的数组中,这样它将作为最后一个执行,并返回modelValue,这样您将“覆盖”角度验证器并将invalidValue绑定到ngModel。

这不是一个bug,这是出于设计。输入类型电子邮件使用正则表达式验证电子邮件地址输入。如果输入无效,将从模型中删除该输入

另一方面,如果你把你的东西包装成一个表单,你可以确定输入的验证状态。另外,当您使用repeat时,您将需要一个索引

以下是我的意思的一个例子:

//[formName].[inputFieldName].property 
myForm.email1.$pristine;
// Boolean. True if the user has not yet modified the form.
myForm.email1.$dirty
// Boolean. True if the user has already modified the form.
myForm.email1.$valid
// Boolean.True if the the form passes the validation.
myForm.email1.$invalid
// Boolean. True if the the form doesn't pass the validation.
myForm.email1.$error
<form name="myForm">
    <div ng-repeat="contact in contacts">
      <div ng-repeat="email in contact.emails track by $index">
        <div ng-show="myForm.email{{$index}}.$invalid">Invalid email.</div>
        <input type="email" name="email{{$index}}" ng-model="email.email">
        <input type="text" name="text" ng-model="email.text">
      </div>
    </div>
  </form>
//[formName].[inputFieldName].property 
myForm.email1.$pristine;
// Boolean. True if the user has not yet modified the form.
myForm.email1.$dirty
// Boolean. True if the user has already modified the form.
myForm.email1.$valid
// Boolean.True if the the form passes the validation.
myForm.email1.$invalid
// Boolean. True if the the form doesn't pass the validation.
myForm.email1.$error