[英]how to call the directive with different templateurl and conroller from another controller function?
我是AngularJS的新手。 我有以下HTML代碼
<div id="products" ng-contoller="productController" listProducts></div>
這將在angular js運行時執行,並且我將在屬性中獲取所有產品。 我的指令如下
learnApp.directive('listproducts',function(){
return{
restrict: 'A',
templateUrl:"products.php",
controller:"productController"
};
});
但是我的購物車中有選定的產品ID
因此,當點擊購物車時,我想將指令替換為所選產品的詳細信息。
<span class='glyphicon glyphicon-shopping-cart' ng-click='productDetails();'>{{totalProductList.totalProducts}}</span>
像下面
productDetails
函數應調用該指令並將其替換為productDetails.php
learnApp.directive('listproducts',function(){
return{
restrict: 'A',
if(shoppingCartImageclicked)
{
templateUrl:"productDetailsForSelectedItems.php",
controller:"productDetailControllerForSelectedItems"
}
else // initial load
{
templateUrl:"productDetails.php",
controller:"productDetailController"
}
};
});
老實說,這似乎是一個非常糟糕的主意。 這不是指令應被使用的方式。 一個指令應該有一項任務。 您的指令有兩個任務,因此應該是兩個指令。 您將必須定義第二條指令並以編程方式(最好是使用javascript)解決視圖的更改,例如通過使用其他路由或至少使用ng-show / ng-if。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.