Angularjs 角度,角度火力-在控制器中获取值
我正在制作web应用程序,从输入文本中获取值时遇到问题。如何从输入中获取它们Angularjs 角度,角度火力-在控制器中获取值,angularjs,angularfire,Angularjs,Angularfire,我正在制作web应用程序,从输入文本中获取值时遇到问题。如何从输入中获取它们 var app = angular.module('app', ['ngRoute', 'firebase']); app.controller("AuthCtrl", function($scope, $firebaseObject) { var ref = firebase.database().ref(); $scope.data = $firebaseObject(ref); }); app.c
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);
});
}
}
]);
解决方案:
使用了错误的控制器。
从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);
});
}
}
]);
您没有提供HTML代码,但我假设您有一个这种格式的表单
<form name="myForm">
<input type="login" ng-model="myForm.login">
<input type="password" ng-model="myForm.pass">
<input type="submit" value="Submit">
</form>
不要在
$scope.login
函数中使用$scope
作为参数名:
app.controller("AuthCtrl", ["$scope", "$firebaseAuth",
function($scope, $firebaseAuth) {
//ERRONEOUS
//$scope.login = function($scope) {
//INSTEAD
$scope.login = function() {
var login = 'user@example.com';
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
对象引用。请参阅。通过将模型与视图以及视图与模型同步,提供双向数据绑定。Angular 1.6.3、Firebase 3.8.0、Angularefire 2.3.0我将遵循以下步骤:
app.controller("AuthCtrl", ["$scope", "$firebaseAuth",
function($scope, $firebaseAuth) {
//ERRONEOUS
//$scope.login = function($scope) {
//INSTEAD
$scope.login = function() {
var login = 'user@example.com';
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);
});
}
}
]);