gpt4 book ai didi

javascript - AngularJS - 指令的 templateUrl 中的表单验证

转载 作者:行者123 更新时间:2023-12-03 23:41:45 25 4
gpt4 key购买 nike

我正在尝试从 templateUrl 内部使用 Angular 的表单验证。

我有一个加载 templateUrl 的指令,其中我有一个表单,其中包含从指令范围获取 ng-required 和 ng-regex 值的输入。现在,我试着把我的指令范围form: '=',但是当我访问 scope.form 时它是未定义的。

我必须指定我的提交按钮在表单之外,当单击 ng-click='save($index)' 时,它必须首先检查表单是否有效,然后继续保存编辑的数据。 scope.save() 在我的指令中定义。

这是来自模板:

 <tr data-ng-repeat="row in source.data "  data-ng-class="{'selected':row.$_selected}" >
<td data-ng-repeat="c in settings.columns" data-ng-click="toggleSelect(row)" >
<form name="editForm" id="editForm" novalidate>
<div ng-switch on="c.type" ng-show="editMode[$parent.$index]">
<span ng-switch-when="text" >
<input type="{{c.type}}" data-ng-model="row[c.name]" ng-required="{{c.isRequired}}" ng-pattern="{{c.regex}}"/>
</span>
<span ng-switch-when="select" >
<select data-ng-model="row[c.name]" ng-selected="row[c.name]" ng-init="row[c.name]" ng-options="item.value as item.name for item in c.items" ng-required="{{c.isRequired}}">
<!--<option data-ng-repeat="(value, name) in c.items" value="{{value}}">{{name}}</option>-->
</select>
</span>
<span ng-switch-when="textarea">
<textarea ng-model="row[c.name]" ng-required="{{c.isRequired}}" ng-pattern="{{c.regex}}">
</textarea>
</span>
<span ng-switch-when="checkbox">
<!--<label for="checkboxInput">{{c.name}}</label>-->
<input name="checkboxInput" type="checkbox" data-ng-model="row[c.name]" ng-true-value="{{c.true}}" ng-false-value="{{c.false}}"/>
</span>
<span ng-switch-default="">
{{row[c.name]}}
</span>
</div>
</form>
<span ng-hide='editMode[$parent.$index]'>{{row[c.name]}}</span>
</td>
<td>
<a href="{{row[settings.specialFields.url]}}" class="btn btn-default opacity75" data-ng-if="row[settings.specialFields.url]">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</td>
<td data-ng-if="row[settings.specialFields.isEditable]">
<button ng-click="edit(row)" ng-show="!editMode[$index]" class="btn btn-primary" >
edit {{$index}}
</button>
<button ng-click="save($index)" ng-disabled="" ng-show="editMode[$index]" class="btn btn-primary">
save
</button>
<button ng-click="cancel($index)" ng-show="editMode[$index]" class="btn btn-default">
cancel
</button>
</td>
</tr>

这是我的指令:

scope: {
settings: '=',
source: '=',
form: '='

},

templateUrl: function (element, attr) {
return attr.templateUrl || 'src/grid.html';
},
link: function (scope, element, attrs) {

scope.editMode = [];

scope.editing = false;
scope.previousData = {};

scope.edit = function(field) {

scope.editing = scope.source.data.indexOf(field);
scope.editMode[scope.editing] = true;
scope.previousData = angular.copy(field);

};

scope.save = function(index){
console.log(scope.form);
scope.editMode[index] = false;

if (scope.editing !== false ) {
//do the saving
scope.editing = false;
}
};



scope.cancel = function(index){
scope.editMode[index] = false;

if (scope.editing !== false) {
scope.source.data[scope.editing] = scope.previousData;
scope.editing = false;
}
}

最佳答案

给你:

Working Form

  • 表单按钮在表单之外,也可能在指令之外。这与 Angularjs 无关。

  • 如您所述,有两个输入,都需要并且都具有正则表达式验证。

  • 有一个带有 templateURL 的指令

这里要记住的重要一点是表单必须有一个名称,然后它被该名称引用,如:scope.myForm 当

您不必像我在 plunker 中那样命名输入字段,但如果您这样做,并且它们都是彼此不同的值,那么您可以这样做:scope.myForm.myInputName.$valid to如果您愿意,可以查看每个输入是否有效。

但是,在所有输入都有效之前,实际的表单不会有效,因此您可能只需要像提供的示例中那样对表单本身调用 valid。

如果将按钮移到指令之外,则必须将提交功能从指令移至 Controller (很可能)。

如果这有帮助,请告诉我,如果需要,我可以进行更改。另外,先用你的问题尝试 plunker,然后发布新代码的情况;只需 fork the plunker 并提供链接即可。

这里是 plunker 代码以防万一......

<!DOCTYPE html>
<html>

<head>
<link data-require="bootstrap@*" data-semver="3.2.0" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" />
<script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script data-require="bootstrap@*" data-semver="3.2.0" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.js"></script>
<script data-require="angular.js@1.3.7" data-semver="1.3.7" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js"></script>
<script data-require="angular-ui-bootstrap@0.12.0" data-semver="0.12.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<div ng-app="myApp">
<div ng-controller="MyCtrl">
<my-directive></my-directive>
</div>
</div>
</html>

<form name="myForm" novalidate>
<label>Input #1 (required)</label><br>
<input ng-model="form.data.myName" name='myName' ng-pattern="/\D+/" ng-required="true" /> <span ng-show="myForm.myName.$error.pattern">Takes anything but digits</span><br>
<br>
<label>Input #2 (required)</label><br>
<input ng-model="form.data.myEmail" name='myEmail' ng-pattern="/\d+/" ng-required="true" /> <span ng-show="myForm.myEmail.$error.pattern">Takes only digits</span>
</form>
<br>
<p># I'm a button that is outside of the form!</p>
<p ng-model="form.submitted">Form Submitted: <span class="blue">{{ form.submitted }}</span></p>
<p>Form Valid?: <span class="blue">{{ myForm.$valid }}</span></p>
<button ng-click="submitForm('myForm')">Submit Form</button>

<p ng-model="form.data">Here is the Form data:</p>
{{ form.data }}


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

app.controller('MyCtrl', function ($scope) {});

app.directive("myDirective", function () {

return {
restrict: 'AE',
require: '?ngModel',
templateUrl: 'my-directive.html',

link: function (scope, elm, attrs, ctrl) {
scope.form = {submitted: false, data: {}};

scope.submitForm = function(formname){
console.log(scope[formname].myEmail)
console.log(scope.formname)
console.log(scope[formname].$valid)
scope.form.submitted = true;
}
} // end link
} // end return
});

关于javascript - AngularJS - 指令的 templateUrl 中的表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24821582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com