我是AngularJS的新手。我正在尝试将控制器作为控制器:
var vm = this;
vm.accounts = [];
vm.authorized = false;
vm.title = 'LoginPage';
我用来在vm.accounts中存储JsonData的函数:
function login() {
try {
return dataservice.login().then(function (data) {
this.accounts = data;
return this.accounts;
})
}
catch (e) {
console.log(e)
}
}
所有这些都是完美的,并且完美无缺。问题是:当我尝试访问这样的vm.accounts时:
function checkLogin(){
for (var i = 0; i < this.accounts.length; i++) {
if (this.accounts[i].name == "Vlad")
this.authorized = true;
}
所有变量,甚至“i”都为空,有什么问题?
这是我的完整.js源代码:
(function () {
'use strict';
angular
.module('app.login')
.controller('login', Login);
/* @ngInject */
function Login(dataservice, logger) {
/*jshint validthis: true */
var vm = this;
vm.accounts = [];
vm.authorized = false;
vm.title = 'LoginPage';
login();
checkLogin();
function login() {
try {
return dataservice.login().then(function (data) {
this.accounts = data;
return this.accounts;
})
}
catch (e) {
console.log(e)
}
}
function checkLogin(){
for (var i = 0; i < this.accounts.length; i++) {
if (this.accounts[i].name == "Vlad")
this.authorized = true;
}
}
}
})();
数据服务:
(function() {
'use strict';
angular
.module('app.core')
.factory('dataservice', dataservice);
/* @ngInject */
function dataservice($http, $location, $q, exception, logger) {
var isPrimed = false;
var primePromise;
var service = {
getAvengersCast: getAvengersCast,
getAvengerCount: getAvengerCount,
getAvengers: getAvengers,
login:login,
ready: ready
};
return service;
function login() {
return $http.get('/api/accounts')
.then(getAccounts)
.catch(function(message) {
exception.catcher('XHR Failed for getAccounts')(message);
$location.url('/');
});
function getAccounts(data,status,headers,config) {
console.log(data.data[0]);
return data.data[0].data.results;
}
}
function getAvengers() {
return $http.get('/api/maa')
.then(getAvengersComplete)
.catch(function(message) {
exception.catcher('XHR Failed for getAvengers')(message);
$location.url('/');
});
function getAvengersComplete(data, status, headers, config) {
return data.data[0].data.results;
}
}
function getAvengerCount() {
var count = 0;
return getAvengersCast()
.then(getAvengersCastComplete)
.catch(exception.catcher('XHR Failed for getAvengerCount'));
function getAvengersCastComplete (data) {
count = data.length;
return $q.when(count);
}
}
function getAvengersCast() {
var cast = [
{name: 'Robert Downey Jr.', character: 'Tony Stark / Iron Man'},
{name: 'Chris Hemsworth', character: 'Thor'},
{name: 'Chris Evans', character: 'Steve Rogers / Captain America'},
{name: 'Mark Ruffalo', character: 'Bruce Banner / The Hulk'},
{name: 'Scarlett Johansson', character: 'Natasha Romanoff / Black Widow'},
{name: 'Jeremy Renner', character: 'Clint Barton / Hawkeye'},
{name: 'Gwyneth Paltrow', character: 'Pepper Potts'},
{name: 'Samuel L. Jackson', character: 'Nick Fury'},
{name: 'Paul Bettany', character: 'Jarvis'},
{name: 'Tom Hiddleston', character: 'Loki'},
{name: 'Clark Gregg', character: 'Agent Phil Coulson'},
{name: 'Clark Gregg', character: 'Agent Phil Coulson'},
{name: 'Clark Gregg', character: 'Agent Phil Coulson'},
{name: 'Clark Gregg', character: 'Agent Phil Coulson'},
{name: 'Clark Gregg', character: 'Agent Phil Coulson'}
];
return $q.when(cast);
}
function prime() {
// This function can only be called once.
if (primePromise) {
return primePromise;
}
primePromise = $q.when(true).then(success);
return primePromise;
function success() {
isPrimed = true;
logger.info('Primed data');
}
}
function ready(nextPromises) {
var readyPromise = primePromise || prime();
return readyPromise
.then(function() { return $q.all(nextPromises); })
.catch(exception.catcher('"ready" function failed'));
}
}
})();
返回的数据:
[
{
"data": {
"results":[
{ "name": "Vlad"},
{"name":"Adi"},
{"name":"Admin"}
]
}
}
]