简体   繁体   English

如何使用“controller as”语法引用命名表单

[英]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.

相关问题 使用“作为控制器”语法时,如何在子控制器中引用父级的作用域? - How do I refer to the scope of a parent inside a child controller when using the “as controller” syntax? 如何使用控制器范围的版本来引用JS中的当前控制器? - How to use controller-scoped version of this to refer to the current controller in JS? 如何在Javascript中引用按顺序命名的HTML画布和图片对象? - How to refer to sequentially named HTML Canvases and Picture Objects in Javascript? 如何在控制器中以角度语法获取父控制器 - How to get the parent controller in controller as Syntax in angular AngularJS - 如何从 ui-router 引用子模块控制器? - AngularJS - How to refer to a submodule controller from ui-router? 如何将依赖项注入命名的角度指令控制器? - How to inject dependencies to a named angular directive controller? 如何动态添加模型表格并将其保存在控制器上 - How to add forms for model dynamically and save it at controller 如何在$ ionic模式下使用'controller as'语法 - How to use the 'controller as' syntax with $ionic modal 如何在Controller As语法中使用带有范围的Ionic Popup? - How to use Ionic Popup with scope in Controller As syntax? 如何使用控制器作为语法在angularjs中收听模型? - How to listen to model in angularjs using controller as syntax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM