我的控制器看起來像這樣不能使用。那麼()NG-點擊函數內部
function menuController($scope, dataService) {
$scope.getMenuItem = '';
$scope.submenu = new Array();
var setMenuItems = function (w) {
$scope.submenu = new Array();
$.each(w, function (i, val) {
if ($scope.getMenuItem == 'year') {
$scope.submenu.push({
name: val[0].year, link: year
});
}
});
}
$scope.UpdateSubmenu = function (a) {
$scope.getMenuItem = a;
loadData();
};
var loadData = function() {
dataService.getAlbums().then(setMenuItems);
}
loadData();
}
時loadData()初始化過程中調用它工作正常。但是,當從UpdateSubmenu調用它時,它將失敗TypeError: dataService.getAlbums(...).then is not a function
從ng-click事件中激活UpdateSubmenu功能。
DataService在看起來像這樣
(function() {
"use strict";
var dataService = function ($http) {
var albums;
var getAlbums = function() {
if (albums)
return albums;
return $http.get("./api/Album").then(function (response) { albums = response.data; return albums; });
}
return { getAlbums: getAlbums };
}
angular.module("Main").factory("dataService", dataService);
}());
爲什麼不能我用then()
不接受?
什麼是'dataService'和'dataService.getAlbums()'?同時顯示這些代碼 – Bikas
現在代碼已添加 – CruelIO