我定义这两个函数:如何使用AngularJS执行字符串作为函数?
function fetchYPosts() {
$http.get("/postsY/")
.then(function(response) {
self.posts = response.data;
}, function(response) {
self.posts = {};
});
};
function fetchXPosts() {
$http.get("/postsX/")
.then(function(response) {
self.posts = response.data;
}, function(response) {
self.posts = {};
});
};
我传递了一个id
和一个字符串(“X”或“Y”是我想要的最终用户传递给我什么)从前端。我有这样的代码,当字符串传递它处理:,
self.handler = function(id, XOrY) {
$http.post("/" + XOrY + "/" + id + "/handle/")
.then(function(response) {
functionToCall = "fetch" + XOrY + "Posts()";
# Here is where I want to call funcitonToCall.
}, function(response) {
self.cerrorMessages = BaseService.accessErrors(response.data);
});
};
这一说给持有字符串的变量,我该怎么称呼它具有字符串变量的名称的功能?
看起来像一个XY问题。你为什么要这样做? – elclanrs
@elclanrs我在前端有两个对象(XPosts和YPosts)。每篇文章都有一个按钮。该按钮应该发布到URL'/ {{X或Y}}/id/handle',然后调用'fetch {{XorY}}()'。 – user2719875