我正在建立一个网站作为学校项目,我正在使用Angularjs和Firebase。
我正在尝试检查用户ID是否位于数组中,但它似乎无法正确比较字符串。
控制器:
'use strict';
var AboutController = angular.module('AboutController', []);
AboutController.factory('Auth', ['$firebaseAuth', function($firebaseAuth) {
var ref = new Firebase('https://MYURL.firebaseio.com/');
return $firebaseAuth(ref);
}]);
AboutController.controller('AboutCtrl', function ($scope, $firebase, Auth) {
var usrRef = new Firebase('https://MYURL.firebaseio.com/Users');
$scope.users = $firebase(usrRef).$asArray();
$scope.auth = Auth;
$scope.account = $scope.auth.$getAuth();
var id = $scope.account.google.id;
var check = false;
for(var user in $scope.users){
if(user.UID === id){
check = true;
break;
}
}
$scope.isUser = check;
if($scope.isUser !== true){
$scope.users.$add({
UID: $scope.account.google.id,
});
}
});
html页面:
<div ng-controller="AboutCtrl">
<div>
<h3>User Data</h3>
<p>{{account.google.id}}</p>
<pre>{{account|json}}</pre>
<p>User has logged in before:{{isUser}}</p>
<pre>{{users|json}}</pre>
<div>
<p>This compares the current UID's in the db to the current user's uid</p>
<div ng-repeat='usr in users'>
<p>{{usr.UID}} {{usr.UID == account.google.id}}</p>
</div>
</div>
</div>
</div>
正如您所看到的那样,html页面与控制器进行相同的比较,但html页面返回正确的答案,而控制器却没有。