【发布时间】:2017-03-24 15:01:07
【问题描述】:
我目前正在尝试实现这个 AngularJS 示例 Tutorial Example Login,但我没有将用户名和密码存储为 字符串,而是尝试从本地文件中提取。
我知道这是一种不好的做法,但我就是这样做的。
在示例中的AngularJS Authentication Service. Path: /modules/authentication/services.js部分中,用户名和密码存储在超时函数中:
$timeout(function(){
var response = { success: username === 'test' && password === 'test' };
if(!response.success) {
response.message = 'Username or password is incorrect';
}
callback(response);
}, 1000);
但我正在尝试创建一个静态 json 文件,该文件将用户名和密码作为对象保存。我的想法是向文件发出 $http.get 请求并将 json 对象附加到用户名和密码参数,如下所示:
var details;
$http.get("data.json").then(function(response){
$scope.details = response.data;
console.log(username);
});
$timeout(function () {
var response = { success: username === details.username && password === details.password
if (!response.success) {
response.message = 'Username or password is incorrect';
}
callback(response);
}, 1000);
但我收到两个错误:
1.ReferenceError: $scope is not defined
2.TypeError: Cannot read property 'username' of undefined
实现我想要做的最简单的方法是什么?提取 来自 json 文件的用户名和密码,而不是将它们作为静态文件 字符串?
【问题讨论】:
标签: javascript jquery angularjs json