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