function getMenus() {
if (!localStorage.getItem('menus')) {
var defer = $q.defer();
$http.get('api/menu').success(function (res) {
// scope.menuList = res;
localStorage.setItem('menus', res)
defer.resolve();
return defer.promise;
}).error(function (err) {
defer.reject()
return defer.promise;
});
}
}
getMenus().then(function () {
scope.menuList = localStorage.getItem('menus')
})
返回结果是angular.min.js:107 TypeError: Cannot read property 'then' of undefined
请问哪里错了。。。。。
The error is reported because the getMenus function has no return value, js returns undefined by default, and then raises then on undefined, so the error is reported
Solution:
Initialized in the first line of getMenus
Finally return defer.promise as the return value
In the end it should look like this
Of course there will be problems if you return promises in the success and error callbacks