Angular 应用程序 Javascript 中的方法链接
Method chaining in Javascript in Angular app
在我的 Angular 网络应用程序中,我有一个名为 ApplicationModule
的模块。
ApplicationModule
具有 get
和 set
功能。
在我的一个控制器中,我调用如下函数:
ApplicationModule.get().then(function (response) {
//do something with response
});
GET()
函数 returns 一个名为 application
的对象。对于返回的对象,我想用它做点什么。所以我使用 then
链接该方法,但我收到一条错误消息 angular.js:13424 TypeError: Cannot read property 'then' of undefined
.
更新了 get() 是什么。
ApplicationModule.get = function () {
if (!localStorage.application) {
Restangular.all('session').one('user').get().then(function (response) {
application = {
"id": response.current_application_id,
"user_id": response.id,
"visa_type": response.current_type
}
localStorage.setItem("application", JSON.stringify(application));
return application
});
} else {
return JSON.parse(localStorage.application)
}
}
我做错了什么?
方法.then()
只适用于promises。为了对您的应用程序对象执行某些操作,我建议将该值存储为变量。
var application = ApplicationModule.get();
foo(application);
如果 localStorage.application
是假的,你的方法 neither return
the application
也不会 return 一个对象。
你在你的函数中做一些异步的事情,所以你需要总是return一个承诺:
ApplicationModule.get = function () {
if (!localStorage.application) {
return Rectangular.all('session').one('user').get().then(function (response) {
// ^^^^^^ returning the promise
var application = {
"id": response.current_application_id,
"user_id": response.id,
"visa_type": response.current_type
};
localStorage.setItem("application", JSON.stringify(application));
return application;
// ^^^^^^ returning the value that the promise will resolve with
});
} else {
return $q.resolve(JSON.parse(localStorage.application));
// ^^^^^^^^^^ creating a promise here as well for consistent interface
}
}
在我的 Angular 网络应用程序中,我有一个名为 ApplicationModule
的模块。
ApplicationModule
具有 get
和 set
功能。
在我的一个控制器中,我调用如下函数:
ApplicationModule.get().then(function (response) {
//do something with response
});
GET()
函数 returns 一个名为 application
的对象。对于返回的对象,我想用它做点什么。所以我使用 then
链接该方法,但我收到一条错误消息 angular.js:13424 TypeError: Cannot read property 'then' of undefined
.
更新了 get() 是什么。
ApplicationModule.get = function () {
if (!localStorage.application) {
Restangular.all('session').one('user').get().then(function (response) {
application = {
"id": response.current_application_id,
"user_id": response.id,
"visa_type": response.current_type
}
localStorage.setItem("application", JSON.stringify(application));
return application
});
} else {
return JSON.parse(localStorage.application)
}
}
我做错了什么?
方法.then()
只适用于promises。为了对您的应用程序对象执行某些操作,我建议将该值存储为变量。
var application = ApplicationModule.get();
foo(application);
如果 localStorage.application
是假的,你的方法 neither return
the application
也不会 return 一个对象。
你在你的函数中做一些异步的事情,所以你需要总是return一个承诺:
ApplicationModule.get = function () {
if (!localStorage.application) {
return Rectangular.all('session').one('user').get().then(function (response) {
// ^^^^^^ returning the promise
var application = {
"id": response.current_application_id,
"user_id": response.id,
"visa_type": response.current_type
};
localStorage.setItem("application", JSON.stringify(application));
return application;
// ^^^^^^ returning the value that the promise will resolve with
});
} else {
return $q.resolve(JSON.parse(localStorage.application));
// ^^^^^^^^^^ creating a promise here as well for consistent interface
}
}