在嵌套UI Bootstrap手风琴中管理范围

时间:2014-03-11 12:42:25

标签: javascript angularjs accordion angularjs-scope angular-ui-bootstrap

我正在使用这个简单的手风琴菜单的UI-Bootstrap。我试图嵌套它们,但我无法获得嵌套元素的范围。

ng-click也不会工作,我猜是因为它已经习惯了打开和关闭手风琴。有没有办法从孩子那里获得父元素?

DEMO

HTML:

<div ng-controller="accCtrl">
<accordion close-others="oneAtATime">
  <accordion-group heading="Classic Accoridion (1)" is-open="$parent.step1">
    <a href="" ng-click="clasClick()">Classic Click</a>
  </accordion-group>
  <accordion-group heading="Nested Accordian (2)" is-open="$parent.step2">
    <accordion close-others="oneAtATime">
      <accordion-group heading="Test (3)" is-open="$parent.step3">
        <a href="" ng-click="nestClick()">Nested Click</a>
      </accordion-group>
    </accordion>
  </accordion-group>
</accordion>
<p>
  Step1: {{step1}}
  <br/>Step2: {{step2}}
  <br/>Step3: {{step3}}
  <br/>
  <a href="" ng-click="openTwo()">Open Two</a>
  <br/>
  <a href="" ng-click="openThree()">Open Three</a>
</p>

JS:

function accCtrl($scope, $http, $timeout) {
    $scope.oneAtATime = true;

    $scope.step1 = true;
    $scope.step2 = false;
    $scope.step3 = false;


    $scope.openTwo = function() {
        $scope.step2 = true;
    }

    $scope.openThree = function() {
        $scope.step3 = true;
    }

    $scope.clasClick = function() {
        console.log("classic clicked");
    }

    $scope.nestClick = function() {
        console.log("nested clicked");
    }
}

1 个答案:

答案 0 :(得分:1)

看起来每个手风琴指令都在创建一个自己的范围。有关详细信息,您必须在https://github.com/angular-ui/bootstrap/blob/master/src/accordion/accordion.js

查看手风琴的实现

要访问父级的父级,您可以键入$ parent。$ parent,如下所示:

<accordion close-others="oneAtATime">
  <accordion-group heading="Classic Accoridion (1)" is-open="$parent.step1">
    <a href="" ng-click="clasClick()">Classic Click</a>
  </accordion-group>
  <accordion-group heading="Nested Accordian (2)" is-open="$parent.step2">
    <accordion close-others="oneAtATime">
      <accordion-group heading="Test (3)" is-open="$parent.$parent.$parent.step3">
        <a href="" ng-click="$parent.$parent.$parent.nestClick()">Nested Click</a>
      </accordion-group>
    </accordion>
  </accordion-group>
</accordion>