我正在尝试在 ng-book jsbin中复制一个示例。
这是我的plnkr
应用程序.js
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.fields = [{placeholder: 'Email', isRequired: true},
{placeholder: 'Password', isRequired: true},
{placeholder: 'Comment(Optional)', isRequired: false}]
$scope.formSubmit = function(){
for (var i=0; i < $scope.fields.length; i++)
{ var obj = $scope.fields[i]
for (var key in obj){
if(obj.hasOwnProperty(key)){
alert(key + ' : ' + obj[key])
}
}
}
}
});
html
<html ng-app="plunker">
<head><script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.22/angular.js" data-semver="1.2.22"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<form name='main_form' ng-submit='formSubmit()' novalidate>
<div ng-repeat="field in fields" ng-form="dynamic_form">
<input type='text'
name='dynamic_input'
ng-required='field.isRequired'
ng-model='field.name'
placeholder='{{field.placeholder}}'>
<div ng-show="dynamic_form.dynamic_input.$dirty && dynamic_form.dynamic_input.$invalid">
<span ng-show="dynamic_form.dynamic_input.$error.required"> This field is required.</span>
</div>
</div>
<button type='submit' ng-diabled="main_form.$pristine && main_form.$invalid">Submit All</button>
</form>
</body>
</html>
我在这里面临的问题:
- 提交按钮未禁用
- 未显示必填字段的错误。
- 如果我点击提交,我也会在警报中获得 $hash 键。这是为什么?
谢谢。