gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'getAccounts' of undefined

转载 作者:行者123 更新时间:2023-11-30 08:38:06 26 4
gpt4 key购买 nike

我正在按照 John Papa 的 Angular Style 指南创建一个小型应用程序,但我似乎无法解决 controller 的问题。使用来自 service 的方法...

模块

(function () {
'use strict';
var accountsApp = angular.module('accountsApp', ['ui.bootstrap', 'ngAnimate']);
})();

Controller

(function() {
'use strict';

angular
.module('accountsApp')
.controller('accountsCtrl', accountsCtrl);

accountsCtrl.$inject = ['$log'];

function accountsCtrl($log, accountsDataSvc) {
/* jshint validthis:true */
var vm = this;
vm.title = 'Accounts';
vm.accounts = [];
vm.accountForm = null;
vm.account = { id: 0, name: '', description: '' }

activate();

function activate() {
return getAccounts(1).then(function() {
$log.info('Accounts loaded');
});
}

function getAccounts(id) {
return accountsDataSvc.getAccounts(id).then(function(data) { //error here
vm.accounts = data;
return vm.accounts;
});
}
}
})();

服务

(function () {
'use strict';

angular
.module('accountsApp')
.factory('accountsDataSvc', accountsDataSvc);

accountsDataSvc.$inject = ['$http', '$log'];

function accountsDataSvc($http, $log, $q) {

var uri = '***';

var service = {
getAccounts: accounts
};

return service;

//////////////////////

function accounts(userId) {
$log.info(uri + userId + ' called');
return $http.get(uri + userId)
.then(getAccountsComplete)
.catch(function (message) {
$log.error('XHR Failed for getAccounts ' + message);
});

function getAccountsComplete(data, status, headers, config) {
return data.data[0].data.results;
}
}
}
})();

当我运行应用程序时,我收到错误 TypeError: Cannot read property 'getAccounts' of undefined in the controller,但我看不出我哪里出错了 - 任何帮助都是非常感谢。

最佳答案

看起来你忘了将 accountsDataSvc 注入(inject)你的 Controller

尝试:

accountsCtrl.$inject = ['$log', 'accountsDataSvc'];

关于javascript - 类型错误 : Cannot read property 'getAccounts' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29649327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com