2017-05-02 5 views
0

웹 앱을 만들고 있는데 입력 텍스트에서 값을 가져 오는 데 문제가 있습니다. 투입물에서 그들을 얻는 방법?각, 각도 - 컨트롤러에서 값을 얻으십시오

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); 
    }); 
    } 
} 
]); 
+0

[AngularJS 개발자 가이드 - 양식 (간단한 양식)] (https://docs.angularjs.org/guide/forms#simple-form)을 참조하십시오. * [ngModel 지시어] (https://docs.angularjs.org/api/ng/directive/ngModel)는 모형을보기뿐만 아니라 모형과 동기화하여 양방향 데이터 바인딩을 제공합니다. * – georgeawg

+0

각도 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

당신이

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

지금이 형식의 양식을 가정 해 봅시다 당신은 HTML 코드를 제공하지 않았다 그러나 나는 생각됩니다 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); 
    }); 
    } 
} 
]); 
$scope.login 함수에서 인수 이름으로 $scope을 사용하지 마십시오
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 객체 참조를 숨 깁니다.