AngularJS错误:TypeError:v2.login不是函数

Nic*_*ick 22 angularjs firebase angularjs-ng-click firebase-authentication

我想在单击登录按钮时调用登录功能,但不断在标题中收到错误消息.有人可以在我的脚本中指出错误吗?

login.js代码如下:

/*global Firebase, angular, console*/

'use strict';
// Create a new app with the AngularFire module
var app = angular.module("runsheetApp");

app.controller("AuthCtrl", function ($scope, $firebaseAuth) {
    var ref = new Firebase("https://xxxxx.firebaseio.com");
    function login() {
        ref.authWithPassword({
            email    : "xxxxx",
            password : "xxxx"
        }, function (error, authData) {
            if (error) {
                console.log("Login Failed!", error);
            } else {
                console.log("Authenticated successfully with payload:", authData);
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

login.html的代码如下:

<div class="container" style="max-width: 300px">
    <form class="form-signin">       
      <h2 class="form-signin-heading" style="text-align: center">Please Sign In</h2>
      <input type="text" class="form-control" name="username" ng-model = "username" placeholder="Email Address" required="" autofocus="" />
        </br>
      <input type="password" class="form-control" name="password" ng-model = "password" placeholder="Password" required=""/>
        </br>
      <button class="btn btn-lg btn-primary btn-block" type="submit" ng-click="login()">Login</button>   
    </form>
  </div>
Run Code Online (Sandbox Code Playgroud)

Aar*_*ray 65

边缘情况在这里,但我想为后人的缘故提及它.当使用name带有相同值的表单的controllerAs模式时,我得到了同样的错误ng-submit.例如:

<form name="authCtrl.signUp" ng-submit="authCtrl.signUp()">

抛出:TypeError:v2.signUp不是函数

解决方案是将表单的名称更改为不同的名称:

<form name="authCtrl.signUpForm" ng-submit="authCtrl.signUp()">

  • 这绝对是我的问题的答案.它表明它并不总是一个'这是所有基本的东西'的情况.谢谢.我不需要我元素上的控制器,因为我使用的是指令. (8认同)

r0u*_*3ck 13

就我而言,我和你的问题完全相同.然而,遇到gkalpak对这种情况的回答帮助了我.

原来我正在调用的是addBuddy()函数,来自名为"addBuddy"的表单.解决方案是改变两个东西中的任何一个的名称,使其脱颖而出或与另一个区别开来.我将表单的名称更改为"addBuddyForm",瞧!我的功能奏效了!

这是我案例的片段:

<form name="addBuddy" class="form-horizontal" novalidate>
...
<button class="btn btn-sm btn-info" ng-click="addBuddy()>Submit</button>
Run Code Online (Sandbox Code Playgroud)

其中,我改为:

<form name="addBuddyForm" class="form-horizontal" novalidate>
...
<button class="btn btn-sm btn-info" ng-click="addBuddy()>Submit</button>
Run Code Online (Sandbox Code Playgroud)

......它工作了!:)


小智 11

在AngularJS中,从视图调用函数它必须在$ scope中.

JS

// exposes login function in scope
$scope.login = login;
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="container" ng-controller="AuthCtrl" style="max-width: 300px"> <!-- I notice here for include ng-controller to your main div -->
<form class="form-signin">       
  <h2 class="form-signin-heading" style="text-align: center">Please Sign In</h2>
  <input type="text" class="form-control" name="username" ng-model = "username" placeholder="Email Address" required="" autofocus="" />
    </br>
  <input type="password" class="form-control" name="password" ng-model = "password" placeholder="Password" required=""/>
    </br>
  <button class="btn btn-lg btn-primary btn-block" type="submit" ng-click="login()">Login</button>   
</form>
Run Code Online (Sandbox Code Playgroud)


And*_*edt 7

这可能不是特定于您的问题,但我也得到了这个错误,并花了一点时间弄清楚原因.

我已经将函数和变量命名为相同的函数,并且在函数中指定了变量,因此变量的赋值覆盖了函数,并且在第二次运行时爆炸了.

您将在示例中注意到uploadFile()函数为upload.uploadFile = true; 这是一个很棒的文件,意思是upload.uploadingFile - 一个用于控制微调器行为的标志.一旦修复,问题就消失了.

例:

(function()
{
  'use strict';

  angular.module('aumApp.file-upload')
  .controller('FileUploadCtrl', FileUploadCtrl);

  function FileUploadCtrl($scope, $http)
  {
    upload.uploadFile = function()
    {
      upload.uploadFile = true;
      var backendUrl = '/ua_aumcore/events/api/v1/events/uploadFile';
      var fd = new FormData();
      fd.append('file', upload.src);
      $http({ url: backendUrl, data: fd, method: 'POST', transformRequest : angular.identity, headers: { 'Content-Type' : undefined } })
      .then(function uploadSuccess(response)
      {
        upload.data = response.data;
        upload.message = "Uploaded Succesfully.";
        upload.uploadSuccess = true;
        upload.uploadingFile = false;
      },
      function uploadFailure(response)
      {
        upload.message = "Upload Failed.";
        upload.uploadSuccess = false;
        upload.uploadingFile = false;
      });
    };
  }
  FileUploadCtrl.$inject = ['$scope', '$http'];
})();
Run Code Online (Sandbox Code Playgroud)


JB *_*zet 5

要从视图中调用,函数必须位于$ scope中.加

$scope.login = login;
Run Code Online (Sandbox Code Playgroud)

到控制器的JS代码.

您还需要实际使用该控制器.更改

<div class="container" style="max-width: 300px">
Run Code Online (Sandbox Code Playgroud)

<div ng-controller="AuthCtrl" class="container" style="max-width: 300px">
Run Code Online (Sandbox Code Playgroud)

这都是基本的东西.我的建议是在进一步学习之前先学习AngularJS教程.