1
比較JSON數據綁定值使用服務,我成功地檢索一個JSON數組,我則抽數據到我的控制器,並試圖用數據綁定值作爲用戶比較輸入了什麼,他們會就像他們的用戶名一樣,但是當值匹配時我沒有顯示日誌。以下是我有:從服務AngularJS
角服務:
loginApp.factory('userData', function($http, $log) {
return {
getUsers: function(scb) {
$http({method: 'GET', url: 'somelink/ajax/current_users.php'}).
success(function(data, status, headers, config) {
$log.info(data, status, headers(), config);
scb(data);
}).
error(function(data, status, headers, config) {
$log.warn(data, status, headers(), config);
});
}
};
});
我的控制器:
loginApp.controller('loginController',
function loginController($scope, userData) {
// If I run a log on the `activeusers`
// I get the same console log as below
userData.getUsers(function(current_users) {
$scope.activeusers = current_users;
});
$scope.createAccnt = {
fname: "",
lname: "",
username: function(inValue) {
for (var i = 0; i < $scope.activeusers.length; i++) {
if ($scope.activeusers[i].username == inValue) {
console.log("Username matches existing!!");
}
}
},
email: ""
};
}
JSON數組返回:
[
{
"user_id": "1",
"username": "userone",
"password": "",
"first_name": "",
"last_name": "",
"email": "",
"active": "",
"auth_token": ""
},
{
"user_id": "2",
"username": "usertwo",
"password": "",
"first_name": "",
"last_name": "",
"email": "",
"active": "",
"auth_token": ""
}
]
控制檯顯示的狀態碼200並顯示所有的數據需求相關捐資,它與此類似:
[Object, Object]
> 0
user_id: "1"
username: "userone"
password: ""
first_name: ""
last_name: ""
email: ""
active: "1"
auth_token: ""
__proto__: Object
> 1
user_id: "2"
username: "usertwo"
password: ""
first_name: ""
last_name: ""
email: ""
active: "1"
auth_token: ""
__proto__: Object
我所有的數據完全結合的作品,只是比較。我的for循環或我的if語句不正確?
是的,它應該感謝你,我的道歉,我固定的但我仍然沒有收到關於比賽的日誌消息。 –
看起來你的代碼中有一部分缺失。什麼觸發createAccnt.userName()? –
這,就是我的問題是,當我試圖迅速成立我已經把結合'createAccnt.username()'輸入標籤的name屬性。現在我已經把它作爲一種'NG-bind'值和控制檯拋出「從一個未定義Cannont獲得價值長度」,我會考慮這一點。我打算將這個答案標記爲已接受。謝謝 –