简体   繁体   English

如何从一个模板的控制器中检索ID,并在另一个模板的控制器中使用它?

[英]How can I retrieve an id from one template's controller and use it in another template's controller?

I have an index.html with two templates, product-list and product-details . 我有一个带有两个模板的index.html ,分别是product-listproduct-details Initially, the index.html displays the product-list template. 最初, index.html显示product-list模板。 Whenever a specific product is clicked, I want to hide this template and show the product-details template. 每当单击特定产品时,我都想隐藏此模板并显示product-details模板。

However, I've got a slight problem. 但是,我有一个小问题。 Aside from doing a full redirect to the product details template with the product id in the url, I'm not sure how to pass the id of the product that is selected. 除了要完全重定向到URL中带有产品ID的产品详细信息模板之外,我不确定如何传递所选产品的id Is it possible to pass the id from the product-list controller to the product-details controller (or directive) without doing something like 是否可以将id从product-list控制器传递到product-details控制器(或指令),而无需执行以下操作

$location.path('details/' + id);

As in can I simply hide the product-list template and show the selected product-details template? 正如我可以简单地隐藏产品列表模板并显示所选的产品细节模板吗?

If the two templates are in the same html file you can hide your product-list template every time a specific product is clicked by setting a variable on your $scope that indicate if a product was clicked... toggle it in your on-click function on each product and then use ng-if / ng-show / ng-hide to show the template you want to show.. 如果两个模板都在同一个html文件中,则可以通过在$ scope上设置一个变量来隐藏每次点击特定产品的产品列表模板,该变量指示是否单击了产品...在单击时将其切换功能,然后使用ng-if / ng-show / ng-hide显示要显示的模板。

if you want to use 2 different controllers you can pass data between them using $stateParams 如果要使用2个不同的控制器,则可以使用$ stateParams在它们之间传递数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个控制器(而不是从模板)访问另一个控制器的属性 - How to access another controller's property from another controller (not from a template) 如何在AngularJS中将控制器的传递值返回到指令的模板属性? - How can I get pass value from controller back to directive's template attribute in AngularJS? 如何在车把模板中获取模型控制器的实例? - How can I get an instance of a model's controller inside a handlebars template? 如何从同一控制器的另一个函数调用Sails.js控制器的函数? - How can I call a Sails.js controller's function from another function of the same controller? 从指令的模板访问命名控制器的方法 - Access named controller's method from directive's template 如何使用涉及控制器的一种方法和另一种方法的promise? - How can I use promise that involves one method from controller and one from another? 控制器的属性未显示在模板上 - Controller's property is not showing up on template 如何将模板中的唯一ID与控制器中的过滤器功能进行匹配? - How match unique id from template to a filter function in controller? 给定AngularJS中的模板和控制器实例,如何使用它的控制器实例编译模板? - Given a template and a controller instance in AngularJS, how can I compile the template in a way that it uses my controller instance? 如何从另一个控制器调用 Loopback4 控制器的方法 - How to call Loopback4 controller's method from another controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM