[英]How to have AngularJS route with same url hit a different controller function?
Very confused and not sure if I am thinking about it correctly. 非常困惑,不确定我是否正确考虑。 Please let me know what else I need to show for this to make sense... 请让我知道我还需要说明什么才能使这个变得有意义...
I have two different functions in mongoose. 我的猫鼬有两个不同的功能。 One indexes all listings and the other indexes unique user listings and is filtered in my mongoose function. 一个索引所有列表,另一个索引唯一的用户列表,并在我的猫鼬函数中过滤。 However, in my routes, how do I hit the unique function when on my profile page if the $http url is the same? 但是,在我的路线中,如果$ http url相同,如何在个人资料页面上点击唯一功能?
//From server.js to controllers
app.get('/api/listings', controllers.listings.index);
//all listings shown on home page
function index (req, res) {
db.Listing.find({}, function(err, allListings) {
res.json(allListings);
});
}
//From server.js to controllers
app.get('/api/listings/', auth.ensureAuthenticated, controllers.profiles.indexUnique);
//unique function that I want to show on profile page only
function indexUnique(req, res) {
db.User.findById(req.user, function (err, user) {
if (err) {console.log(err);}
db.Listing.find({uid: req.user}, function(err, listings) {
if(err) { console.log('Error', err); }
res.json(listings);
});
})
}
How do I adjust my http request to hit my unique function if the url routes are the same? 如果网址路由相同,如何调整我的http请求以发挥我的独特功能?
// Angular http request in my home controller
$http({
method: 'GET',
url: '/api/listings'
}).then(function successCallback(response) {
vm.listings = response.data;
}, function errorCallback(response) {
console.log('Error getting data', response);
});
您可以将角作为服务进行过滤,并保留两个单独的对象数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.