简体   繁体   English

如何使具有相同URL的AngularJS路由命中不同的控制器功能?

[英]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.

相关问题 如何为同一路径调用不同的控制器功能? - How to call different controller function for same route? AngularJS正则表达式路由为类似的URL加载不同的控制器和视图 - AngularJS regex route for similar url to load different controller and view 我应该如何为每个ajax函数调用相同的ajax路由URL,即使它位于laravel 5.5中的另一个控制器中 - How should I call same ajax route url for every ajax function even if it is in another controller in laravel 5.5 如何路由到同一位置以重新启动视图和控制器绑定(angularjs)? - How to route to same location to restart view and controller binding (angularjs)? 如何基于路由在具有不同变量的angularjs中重用控制器 - How to reuse a controller in angularjs with different variables based on route 如何从其他控制器AngularJS调用控制器中的函数 - How to call a function in a controller from a different controller AngularJS 无法使用angularjs路由到具有其他协议的URL - Cannot route to a url with a different protocol with angularjs AngularJS,具有不同模板但具有相同控制器的多个路由怎么样? - AngularJS, How about multiple routes with different templates but the same controller? 如何在 React JS 中制作相同的 URL 方案但不同的路线和组件 - How to make same URL scheme but different route and component in React JS AngularJs如何在控制器中清理URL? - AngularJs How to sanitize URL in the controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM