AngularJs范围参数未定义

时间:2018-07-04 00:12:32

标签: javascript angularjs angularfire

我刚刚开始学习AngularJS,我正在尝试使用简单的输入,控制器和firebase数据库来制作这个简单的应用程序。

问题是,单击按钮时, Signin()中始终没有定义$ scope参数 userEmail userPassword

HTML相关代码:

<body ng-app="indexApp">
...
<div class="row" ng-controller="loginCtrl">
    <div class="hero-text-box">
        <br>
        <form>
            <p class="p2"><label for="email">email</label></p>
            <input ng-model="userEmail" id="email1" type="email"
                   name="email1" placeholder="email">
            <p class="p2"><label for="password">pasword</label></p>
            <input ng-model="userPassword" type="password" id="password"
                   name="password" placeholder="password" required>
            <a class="btn btn-full" href="#" ng-click="Signin()">הכנס</a>
        </form>

    </div>
</div>

AngularJS javascript代码:

var app = angular.module("indexApp", ["firebase"]); // using firebase

app.controller("loginCtrl", function($scope, $firebaseArray) // AngularJS 
will auto add firebase 
{
var ref = firebase.database().ref().child("users")
// create a synchronized array
$scope.users = $firebaseArray(ref)

$scope.Signin = function()
{
    console.log($scope.userEmail) -- undefined!
    ....

2 个答案:

答案 0 :(得分:0)

这可能有帮助

  

HTML

<body ng-app="indexApp">
...
<div class="row" ng-controller="loginCtrl">
    <div class="hero-text-box">
        <br>
        <form>
            <p class="p2"><label for="email">email</label></p>
            <input ng-model="userdata.userEmail" id="email1" type="email"
                   name="email1" placeholder="email">
            <p class="p2"><label for="password">pasword</label></p>
            <input ng-model="userdata.userPassword" type="password" id="password"
                   name="password" placeholder="password" required>
            <a class="btn btn-full" href="#" ng-click="Signin(userdata)">הכנס</a>
        </form>

    </div>
</div>
  

控制器

var app = angular.module("indexApp", ["firebase"]); // using firebase

app.controller("loginCtrl", function($scope, $firebaseArray) // AngularJS 
will auto add firebase 
{
var ref = firebase.database().ref().child("users")
// create a synchronized array
$scope.users = $firebaseArray(ref)
$scope.userdata = {};

$scope.Signin = function(data){
 console.log(data) // you will get the object 
}

答案 1 :(得分:0)

如果要在angularJS中使用表单,则应遵循angularjs表单指南:https://docs.angularjs.org/guide/forms

以您为例,您需要将signin()函数移动到html表单元素。我根据您的代码http://jsfiddle.net/sxwei123/ADukg/25254/创建了一个jsfiddle

希望这会有所帮助!