I learned from the official documentation that when a name is specified for a form, the form will be added to the scope, and the form object can be obtained by using the name of the form; at the same time, the control object can also be obtained from the form object based on the name of the control.
Such as:
<form role="form" name="infoForm">
<input name="name" class="xxx" ng-model="xxx">
</form>
In controller
var form = $scope.infoForm;
//如果控件内容发生改变
if(form.name.$dirty) {
...
}
The problem we are encountering now is:
There is a series of controls generated through ng-repeat, I have tried "name = xx{{$index}}" to specify different controls,
<form name="adminForm">
管理员名单
<p ng-repeat="manager in theEditingRobot.managers">
<p class="cx-inputer">
<input name="manager{{$index}}" type="email" placeholder="请输入邮箱"
ng-model="manager.val" ng-blur="checkManager($index)">
</p>
</p>
</p>
It turns out that although the control names generated by ng-repeat can be seen in the html page as xxx0, xxx1, etc., when looking at the properties of the form object, it is found that there are no attributes such as xxx0, xxx1, etc., but only xxx{{$ index}} attribute, and this attribute represents the last input control generated by ng-repeat. As shown in the picture,
html page:
As shown in the figure, ng-repeat generated 2 input controls
But when debugging in the controller, I found that there are no manager0 and manager1 attributes in the form, but only manager{{$index}}, which means manager1
The inference is that ng-repeat expansion occurs after the form is added to the scope behavior. So, how can we obtain all control objects through form? Why does $scope.adminForm display Constructor instead of Object?
PS: I am using the framework automatically generated by yeoman
PPS: I also found that the form displays instantiate.c, but the above problem still occurs
JSFiddle link: https://jsfiddle.net/xwadh6kt/1/
<input name="xxx{{$index}}">
Note that the name of a form element can only be a string, and data types such as arrays or objects cannot be constructed. However, variables in strings can be parsed using double curly brackets.
I was working on something similar when I was answering. The dynamically generated form group uses
ngRepeat
traversal to show you the actual effect:This is the corresponding source code:
I used this method to obtain unique values for both id and name.
The reason is the version problem of angularjs. I used version 1.4 and the above problems did not occur,
For details, see: http://stackoverflow.com/questions/24020503/angular-ng-repeat-with-ng-...