javascript - angular.js directive with isolate scope - Stack Overflow

I know there are already a lot of questions about this topic but I can't make my example work.See

I know there are already a lot of questions about this topic but I can't make my example work.

See the example at plnkr.co#ua32dkF7fz6X2fA0ZAw3.

Each teaser has some additional informations that should appear when a user clicks on one of the teasers. However as soon as I click on one of the teasers the additional informations appear below every teaser. I'd like to show them only below the one teaser that I clicked on.

I'm pretty sure that's happening because the directives share the same parent scope. How can I keep the isActive variable private to each teaser?

I know there are already a lot of questions about this topic but I can't make my example work.

See the example at plnkr.co#ua32dkF7fz6X2fA0ZAw3.

Each teaser has some additional informations that should appear when a user clicks on one of the teasers. However as soon as I click on one of the teasers the additional informations appear below every teaser. I'd like to show them only below the one teaser that I clicked on.

I'm pretty sure that's happening because the directives share the same parent scope. How can I keep the isActive variable private to each teaser?

Share Improve this question asked Feb 13, 2013 at 13:50 zemircozemirco 16.4k8 gold badges67 silver badges100 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I would do it like this:

1) html changes:

<teaser ng-repeat="teaser in teasers" teaser="teaser"></teaser>

2) changes to the directive:

restrict: 'E',
replace: true,
scope: {
  teaser: '='
},
controller: function ($scope) {
  $scope.isActive = false;
  $scope.select = function(teaser) {
    $scope.isActive = !$scope.isActive;
  };
}

demo: http://jsbin./aqehew/1/

This should do it: http://plnkr.co/edit/EfB3n14Hwk5uijsrp1aR

What I did was to create a new scope for the directive, and moved the active variable to the directive scope.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745077759a4609935.html

相关推荐

  • javascript - angular.js directive with isolate scope - Stack Overflow

    I know there are already a lot of questions about this topic but I can't make my example work.See

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信