2016-04-22 256 views
0

首先我想要的: 我有一系列的縮略圖。當我點擊其中一個時,我想讓該特定縮略圖顯示在一個更大的div中,並附上其描述。 (以後:應該是動畫)。angularjs:從指令設置範圍變量

現在我有指令和控制器,但我不知道如何設置適當的變量!

所以一些代碼: 第一個HTML:這裏是本節的根文件.jade。我有一個叫做product的指令。

section 
    .detail-view(ng-show="vm.showDetail") 
     .prod-desc(ng-bind="vm.detailPic") 
     .prod-img(ng-bind="vm.detailDesc") 
    product.col-xs-12.product_tile(ng-repeat="item in vm.products", item="::item") 

正如您所看到的,產品指令是ng-repeat的一部分;由於這個原因,div我想顯示調整後的圖像是以外的迭代(.detail-view)。

產品指令:

'use strict'; 
ProductDirective.$inject = ['ShopCart', '$animate', 'User']; 

function ProductDirective(ShopCart, $animate, User) { 
    return { 
     restrict: 'E', 
     scope: { 
      item: '=' 
     }, 
     template: require('./product.html'), 
     link: link 
    }; 

    function link(scope, element) {    
     scope.toggleDetails = toggleDetails; 
    } 

    function toggleDetails() { 
      if (!scope.isSelected && isBlurred()) return; 

      scope.vm.detailPic = scope.item.photo; 
      scope.vm.detailDesc = scope.item.prod_description; 
      scope.vm.isSelected = !scope.isSelected; 
      scope.showDetail = !scope.showDetail; 

      var action = scope.isSelected ? 
    } 
} 

現在我想在大的圖像更新DIV是迭代 - 因此外部指令的範圍。我如何設置showDetail,showDesc和​​的值?

由於我使用controllerAs與價值vm,我想我可能只是做scope.vm.detailPic = scope.item.photo;,如其他解決方案我已經看到,設置根對象的屬性時,它會被傳播......但我得到

無法設置的不確定

回答

0

財產 'detailPic' 現在,什麼工作(但看起來有點奇怪我)是這樣的,在toggleDetails()

scope.$parent.$parent.vm.detailPic = scope.item.photo; 
scope.$parent.$parent.vm.detailDesc = scope.item.prod_description; 
scope.$parent.$parent.vm.showDetail = !scope.$parent.$parent.vm.showDetail