道歉,如果这是显而易见的我有点像Angular \ Javascript noob。我有以下服务:
if let objects = objects {
var indexPaths = [NSIndexPath]
for object in objects {
println("The value of object.objectId is ( object.objectId)")
self.funlists.append("\(object.objectId)")
indexPaths.append(NSIndexPath(row: self.funlists.count - 1, section: 0))
}
self.tableView.insertRowsAtIndexPaths(indexPaths, rowAnimation: .Default)
}
在this.setCurrentClient函数中,我通过$ http调用后端,并在异步成功返回时通过变量getCurrentClient调用get函数。我最初试图直接调用get()或this.get()并且都没有工作。 .success回调函数中的上下文似乎是全局窗口上下文而不是服务上下文。上面的解决方案对我来说似乎有些混乱。有没有更好的方法来获取服务上下文并调用get()而不是使用方法设置变量(var getCurrentClient = this.get())然后调用它(getCurrentClient.then())? javascript \ angular scope和contexts有点迷失......
答案 0 :(得分:0)
我相信你不需要使用"这个"在您的服务中,您可以将您的函数声明为function get() { ...
,然后在setCurrentClient的回调中调用:
var getCurrentClient = get();
但是,如果你真的需要"这个",常见的模式是使用var self = this;
或类似的东西 - 请参阅this discussion。
(另外,我还建议您将函数和变量重命名为var clientPromise = getCurrentClient();
以提高可读性。)
答案 1 :(得分:0)
我想说在这种情况下使用angular factory
。
MyApp.factory('ClientService', function ClientFactory($http, $q, SharedData) {
function get() {
var deferred = $q.defer();
$http.get("/api/Client/")
.success(function(data, status) {
deferred.resolve(data);
})
.error(function(data, status) {
deferred.reject(data);
});
return deferred.promise;
};
function setCurrentClient(clientToSelect) {
var deferred = $q.defer();
var getCurrentClient = get();
$http({ method: "POST", url: "/api/Client", data: clientToSelect })
.success(function (data, status) {
//Get the full selected client and all its properties rather than the list version
getCurrentClient.then(
function (client) {
setCurrentClient(client);
setCurrentPeriod(client);
},
function (data) {
//uho something broke.
});
deferred.resolve(data);
})
.error(function (data, status) {
deferred.reject(data);
});
return deferred.promise;
}
.....
// Expose the service methods
return {
get: get,
setCurrentClient: setCurrentClient
...... (Expose what all methods you wanted to expose)
};
});
答案 2 :(得分:0)
以下是我将如何使用服务的方式..当你试图在http请求中调用this.get然后这个将在Window范围内查找get方法,如果你打印这个在控制台中你可以看到没有get方法的关键字。 此始终引用您正在使用的对象 此
MyApp.service('ClientService', function ($http, $q, SharedData) {
var get = function () {
var deferred = $q.defer();
$http.get("/api/Client/")
.success(function (data, status) {
deferred.resolve(data);
})
.error(function (data, status) {
deferred.reject(data);
});
return deferred.promise;
};
var setCurrentClient = function (clientToSelect) {
var deferred = $q.defer();
$http({
method: "POST",
url: "/api/Client",
data: clientToSelect
})
.success(function (data, status) {
//Get the full selected client and all its properties rather than the list version
get().then(
function (client) {
setCurrentClient(client);
setCurrentPeriod(client);
},
function (data) {
//uho something broke.
});
deferred.resolve(data);
})
.error(function (data, status) {
deferred.reject(data);
});
return deferred.promise;
}
this.get = get;
this.setCurrentClient = setCurrentClient;
});)
.error(function (data, status) {
deferred.reject(data);
});
return deferred.promise;
}
this.get = get;
this.setCurrentClient = setCurrentClient;
})