Angular Directive - 无法在树视图中访问子节点或孙节点

时间:2014-07-25 23:28:11

标签: javascript angularjs angularjs-directive

我有一个构建树视图的指令。它基于http://jsfiddle.net/KNM4q/113/的代码示例。我希望能够单击树中的任何节点,然后单击执行控制器中的方法,该控制器处理特定于该节点的数据。虽然我已经让代码在根节点和子节点中工作,但我不能让它与孙子或伟大的granchildren节点一起工作。

以下是摘要:

http://plnkr.co/edit/AKiD8ZyKK8dUSPEcWI3p?p=preview

这是HTML:

<div ng-controller="appCtrl">

  <tree val="treeData" zo="itemDetail(param)"  ></tree>

</div>

这是指令:

angular.module('components')
    .directive('tree', function ($compile) {
    return {
        restrict: 'E',
        terminal: true,
        scope: { val: '=', parentData:'=', zo:'&' },
        link: function (scope, element, attrs) {
            var template = '<span>{{val.text}}</span><button  ng-click="showDetail()"  ng-show="val.text">Show Detail</button> ';
            if (angular.isArray(scope.val.items)) {
                template += '<ul class="indent"><li ng-repeat="item in val.items"><tree val="item" parent-data="val.items" zo=zo({param:item.text})   >  </tree></li></ul>';
            }
            scope.showDetail = function(index) { 
                var param = 'aaa';
                scope.zo(); 

            };
            var newElement = angular.element(template);
            $compile(newElement)(scope);
            element.replaceWith(newElement);            
        }


    }
});

这是控制器:

function appCtrl($scope) {
    var treeData = {
        "text": "root",
        "items": [{
            "text": "Furniture",
                "items": [{
                "text": "Tables & Chairs"
            }, {
                "text": "Sofas",
                    "items": [{
                    "text": "Tables & Chairs"
                }, {
                    "text": "Sofas"
                }]
            }, {
                "text": "Occasional Furniture"
            }]
        }, {
            "text": "Decor",
                "items": [{
                "text": "Bed Linen"
            }, {
                "text": "Curtains & Blinds"
            }, {
                "text": "Carpets"
            }]
        }]
    };

    //initial parameter that is sent
    $scope.param = 'root';

    $scope.treeData = treeData;

    $scope.itemDetail = function (param){
        alert('in controller ' + param);
    }
}

1 个答案:

答案 0 :(得分:1)

您希望将该函数作为对象引用传递,并使用=而不是&在指令范围内将其绑定回每个父级别。

HTML

<!-- notice removed "()" -->
<tree val="treeData" zo="itemDetail"  ></tree>

JS

由于zo绑定到父作用域函数,因此可以在模板html中使用它

.directive('tree', function ($compile) {
    return {
        restrict: 'E',
        terminal: true,
        scope: { val: '=', parentData:'=', zo:'=' },
        link: function (scope, element, attrs) {
            var template = '<span>{{val.text}}</span><button  ng-click="zo(val.text)"  ng-show="val.text">Show Detail</button> ';
            if (angular.isArray(scope.val.items)) {
                template += '<ul class="indent"><li ng-repeat="item in val.items"><tree val="item" parent-data="val.items" zo="zo"   >  </tree></li></ul>';
            }

            var newElement = angular.element(template);
            $compile(newElement)(scope);
            element.replaceWith(newElement);            
        }


    }

简单来说,这与做:

相同
function foo( param ){
   alert(param);
}

var bar = foo;

bar('someString') // alerts "someString"

DEMO