简体   繁体   中英

Angular bind directive scope dynamically

How can i bind new scope to the directive?

For exmaple we have product catalog and if you click the product - popup will be shown. The main part is that i don't want to create 100 popups that will be hidden, and open the, by something like model.id

All i want to do is to bind some model to the popup on click on the product thumb.

<li ng-repeat="product in products">
   <button ng-click="openPopup(product)"></button>
</li>


// Some controller
... 
$scope.openPopup = function(product) {
    var popup = angular.element('<popup product="product"></popup>');

    // Of course is not working because i want to bind this `product` argument
    $compile(popup)($scope);
}

Could someone tell me how to deal with it? Thanks

I suppose in the sample that we only display one popup at a time and that the popup is modal

Template:

<li ng-repeat="product in products">
   <button ng-click="openPopup(product)"></button>
</li>

<popup ng-show="showPopup" product="selected_product"></popup>

Controller:

$scope.openPopup = function(product) {
   $scope.selected_product = product;
   $scope.showPopup = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM