-2

我有个问题。我需要知道如何在 AngularJS 中获取祖父母的 ID。我需要“{{parent}}”成为“祖父母”。(应该是<div id="me-and-my-grand-parent">

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

    var pid = document.getElementsByClassName("i-am-a-child");

    var pid = this.parentNode.id;

if (this.parentNode&&this.parentNode.id)
var pid=this.parentNode.id;

$scope.parent = var pid;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
    <div id="grand-parent{{$index}}" ng-repeat="item in items">
        <div>
          <div>
            <div id="me-and-my-{{parent}}" class="i-am-a-child">
            </div>          
          </div>
        </div>
      </div>
</div>

我的实际代码

<li ng-repeat="project in projects" ng-class="{active: project.childToggle, '': !project.childToggle,hasChild: project.children.length > 0 }" ng-dblclick="childToggleCt(project)" id="project-{{$index}}">
  <div class="project-overview">
    <header class="clearfix flip-area">
      <span ng-if="!project.inCart" class="status dropdown-button warning pull-left" id="id-{{ParentIdShow}}" data-intro="Status bar" data-position="right">Pending</span>

现在 JS 就像 tis :

$scope.ParentIdShow = function(obj) { alert(obj.target.parentNode.parentNode.parentNode.id); }

4

1 回答 1

0

对这些类型的“我的父母的父母……我的父母的父母”的答案是使用controller as语法。在此处阅读更多相关信息。简而言之,它可以让你做类似的事情

<div ng-controller="ctrl1 as first">
  <div ng-controller="ctrl2 as second">
    ...
        <div ng-controller="ctrlN as Nth">
          <div ng-repeat="i in arr">
              {{first.property}}
              {{second.otherProperty}}
              {{Nth.nProperty}}

请注意您不需要任何parent电话。

于 2015-03-19T19:40:27.667 回答