[英]How to refer to named forms with “controller as” syntax
I'm having trouble referring to named forms in my controller when using the "controller as" syntax in angularjs. 在angularjs中使用“controller as”语法时,我在控制器中引用命名表单时遇到问题。 For example, given the following HTML:
例如,给定以下HTML:
<div ng-controller="MyController as ctl">
<form role="form" name="newItemForm">
<input type="text" id="firstName" ng-model="ctl.firstName"/>
</form>
</div>
In the context of the controller, 在控制器的上下文中,
function MyController() {
var self = this;
console.log(self.newItemForm);
}
self.newItemForm
is undefined. self.newItemForm
未定义。 If I had been using the $scope convention, I could have referred to $scope.newItemForm. 如果我一直在使用$ scope约定,我可以引用$ scope.newItemForm。 Is there any other way of doing this in the controller as syntax without using the scope?
有没有其他方法在控制器中执行此操作作为语法而不使用范围?
Change your HTML to this: 将您的HTML更改为:
<div ng-controller="MyController as ctl">
<form role="form" name="ctl.newItemForm">
<input type="text" id="firstName" ng-model="ctl.firstName"/>
</form>
</div>
Then you will be able to access the named form as expected in your controller without injecting $scope
. 然后,您将能够在控制器中按预期方式访问命名表单,而无需注入
$scope
。 Found this information here: http://www.technofattie.com/2014/07/01/using-angular-forms-with-controller-as-syntax.html 在此处找到此信息: http : //www.technofattie.com/2014/07/01/using-angular-forms-with-controller-as-syntax.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.