2016-04-22 250 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