我正在阅读 ng-form 的 Angular 文档,它说
form - directive in module ng Directive that instantiates FormController.
听起来不错。当我查看example时但是,它显示的是这样的:
<form name="myForm" ng-controller="FormController" class="my-form">
ngController
指定的FormController
与表单自动实例化的是否相同?
谢谢。
最佳答案
它们是两个不同的 Controller 。
通过 ng-controller
声明的 Controller 允许您将数据绑定(bind)到 Controller 的范围,或直接绑定(bind)到 Controller 中的表单对象。
HTML:
{{userType}}
<input ng-model="myForm.userType" />
Controller :
$scope.userType = 'guest';
$scope.myForm.userType = 'guest';
FormController通过 $isDirty、$setPristine 等方法控制表单的状态,并为您维护表单的行为,而无需您这样做。
HTML:
{{myForm.$isDirty()}}
Controller :
$scope.myForm.$setPristine();
请注意,在文档的第一个实例中,直接引用 userType,因为它存在于 ng-controller 绑定(bind)的任何 Controller 的范围内。在第二个实例中,我们引用表单对象本身的范围,myForm
,它在幕后自动绑定(bind)到您的 Controller 范围。
关于javascript - Angular - FormController 是否被 ng-controller 覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32485175/