简体   繁体   English

AngularJS在获取HTML元素的控制器中引用表单

[英]Angularjs referencing a form in a controller getting html element

I have a form named "loginForm" that has a controller "loginController" which has the method "login" set as the submit for the form. 我有一个名为“ loginForm”的表单,该表单具有一个控制器“ loginController”,该控制器的方法“ login”设置为表单的提交。 Inside of the login method in the javascript if I reference the form without referencing it with scope ; 如果我引用表单而不用范围引用它,则在javascript中的login方法内部; so doing "loginForm" instead of "$scope.loginForm"; 这样做是“ loginForm”而不是“ $ scope.loginForm”; I am getting a reference to the html element and if I check the window.loginForm I am finding that this is a global variable. 我正在获取html元素的引用,并且如果我检查window.loginForm我发现这是一个全局变量。 I'm trying to figure out how this variable is getting created and placed into the global scope. 我试图弄清楚如何创建此变量并将其放入全局范围。 Please the plnkr link below for an example of what I am referring to. 请通过下面的plnkr链接获取我所指的示例。

http://plnkr.co/YY3ls7ele8uNpnhstG3X http://plnkr.co/YY3ls7ele8uNpnhstG3X

Controller 控制者

(function(){
  angular.module('testApp',[])
    .controller('loginController',['$scope',function($scope){

      $scope.login = function(){
        console.debug(loginForm); //console shows html element
        console.debug(window.loginForm); //shows html element
      }  
    }]);
  })();

If you are using form then you would need to pass it in fromthe view. 如果您正在使用form则需要从视图中传递它。

<form name="loginForm"  class="form-horizontal" ng-submit="login(loginForm)">

and get it in your method:- 并以您的方法获取它:

$scope.login = function(loginForm){

Or use ng-form="formName" to get the form controller instance as a part of the scope, but there is an issue with 1.2 version of angular where ng-submit does not get trigerred on ng-form. 或使用ng-form="formName"将表单控制器实例作为作用域的一部分,但是angular的1.2版本存在一个问题,即ng-submit在ng-form上不会被触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM