2017-05-02 61 views
0

我正在製作網絡應用程序,並且在從輸入文本中獲取值時遇到問題。如何從輸入中獲取它們?Angular,angularfire - 在控制器中獲得價值

var app = angular.module('app', ['ngRoute', 'firebase']); 

app.controller("AuthCtrl", function($scope, $firebaseObject) { 
    var ref = firebase.database().ref(); 
    $scope.data = $firebaseObject(ref); 
}); 

app.controller("AuthCtrl", ["$scope", "$firebaseAuth", 
function($scope, $firebaseAuth) { 
    $scope.login = function($scope) { 
    var login = ''; 
    var pass = ''; 

    $scope.authObj = $firebaseAuth(); 
    $scope.authObj.$signInWithEmailAndPassword(login, pass) 
    .then(function(firebaseUser) { 
     console.log("Signed in as:", firebaseUser.uid); 
    }).catch(function(error) { 
     console.error("Authentication failed:", error); 
    }); 
    } 
} 
]); 

THE SOLUTION: 錯誤的控制器使用。 從AuthCtrl到loginCtrl一切正常! 一切都因爲我使用ngRoute。

app.controller("logintCtrl", ["$scope", "$firebaseAuth", 
function($scope, $firebaseAuth) { 
    $scope.login = function($scope) { 
    var login = ''; 
    var pass = ''; 

    $scope.authObj = $firebaseAuth(); 
    $scope.authObj.$signInWithEmailAndPassword(login, pass) 
    .then(function(firebaseUser) { 
     console.log("Signed in as:", firebaseUser.uid); 
    }).catch(function(error) { 
     console.error("Authentication failed:", error); 
    }); 
    } 
} 
]); 
+0

請參閱[AngularJS開發人員指南 - 表單(簡單表單)](https://docs.angularjs.org/guide/forms#simple-form)。 * [ngModel指令](https://docs.angularjs.org/api/ng/directive/ngModel)通過將模型同步到視圖以及查看模型來提供雙向數據綁定。* – georgeawg

+0

Angular 1.6.3,Firebase 3.8.0,Angularefire 2.3.0 –

+0

我正在關注這個:https://github.com/firebase/angularfire/blob/master/docs/guide/user-auth.md#signing-用戶,在 –

回答

0

你沒有提供的HTML代碼,但我會假設我們說你在這個格式

<form name="myForm"> 
<input type="login" ng-model="myForm.login"> 
<input type="password" ng-model="myForm.pass"> 
<input type="submit" value="Submit"> 
</form> 

現在有一個表格JS應該

。注意到的表格名稱。您還應該注意,Firebase只接受電子郵件地址以及其他用於驗證的字符串。這意味着第一個字符串必須是電子郵件格式

var app = angular.module('app', ['ngRoute', 'firebase']); 

app.controller("AuthCtrl", function($scope, $firebaseObject) { 
    var ref = firebase.database().ref(); 
    $scope.data = $firebaseObject(ref); 
}); 

app.controller("AuthCtrl", ["$scope", "$firebaseAuth", 
function($scope, $firebaseAuth) { 
    $scope.login = function($scope.myForm) { 
    var login = $scope.myForm.login; 
    var pass = $scope.myForm.pass; 



    $scope.authObj = $firebaseAuth(); 
    $scope.authObj.$signInWithEmailAndPassword(login, pass) 
    .then(function(firebaseUser) { 
     console.log("Signed in as:", firebaseUser.uid); 
    }).catch(function(error) { 
     console.error("Authentication failed:", error); 
    }); 
    } 
} 
]); 
0

不要在$scope.login功能使用$scope作爲參數名:

app.controller("AuthCtrl", ["$scope", "$firebaseAuth", 
function($scope, $firebaseAuth) { 
    //ERRONEOUS 
    //$scope.login = function($scope) { 
    //INSTEAD 
    $scope.login = function() { 
    var login = '[email protected]'; 
    var pass = '12345'; 

    $scope.authObj = $firebaseAuth(); 
    $scope.authObj.$signInWithEmailAndPassword(login, pass) 
    .then(function(firebaseUser) { 
     console.log("Signed in as:", firebaseUser.uid); 
    }).catch(function(error) { 
     console.error("Authentication failed:", error); 
    }); 
    } 
} 
]); 

通過使用$scope作爲$scope.login的第一個參數,它隱藏注入控制器構造函數中的$scope對象引用。