从嵌套指令的指令访问控制器范围

时间:2013-01-31 09:39:59

标签: angularjs

请查看我的fiddle并帮我找到一种从嵌套指令“choice”访问函数myAlert的方法。找到了一些解决方案,其中共享了一些范围属性,如下所示:http://jsfiddle.net/zbD95/6/但我需要同时使用范围内的函数和属性。

感谢!!!

这是我小提琴的副本: HTML小提琴部分:

<!doctype html>
<html ng-app="plunker" >
<head>
  <meta charset="utf-8">
  <title>AngularJS Plunker</title>
  <link rel="stylesheet" href="style.css">
</head>
<body ng-controller="MainCtrl">
  <choice-tree ng-model="myTree"></choice-tree>  
  <hr />
  <!--$scope.myTree = {{myTree | json}}-->
</body>
</html>

小提琴的JS部分:

var app = angular.module('plunker', []);

function Choice(name, children) {
  this.name = name;
  this.checked = false;
  this.children = children || [];
}

var apparel = new Choice('Apparel', [
  new Choice('Mens Shirts', [
    new Choice('Mens Special Shirts'),
  ]),
  new Choice('Womens Shirts'),
  new Choice('Pants')
]);
var boats = new Choice('Boats');

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';

  $scope.myTree = [apparel, boats]; 

  $scope.myAlert = function(ev){
    alert('ad');
  };
});

app.directive('choiceTree', function() {
      return {
          template: '<ul><choice ng-repeat="choice in tree"></choice></ul>',
        replace: true,
        transclude: true,
        restrict: 'E',
        scope: {
          tree: '=ngModel'
        }
      };
});

app.directive('choice', function($compile) {
  return { 
    restrict: 'E',
     transclude: true,
    //In the template, we do the thing with the span so you can click the 
    //text or the checkbox itself to toggle the check
    template: '<li>' +
      '<span ng-click="myAlert(choice)">' +
      '<input type="checkbox" ng-checked="choice.checked"> {{choice.name}} - {{name}}' +
      '</span>' +
    '</li>',
      link: function (scope, elm, attrs) {

      //Add children by $compiling and doing a new choice directive
      if (scope.choice.children.length > 0) {
        var childChoice = $compile('<choice-tree ng-model="choice.children"></choice-tree>')(scope)
        elm.append(childChoice);
      }
    }
  };
});

1 个答案:

答案 0 :(得分:9)

使用'&amp;'语法允许隔离范围调用父范围上定义的方法。此外,您不需要对这些指令进行任何转换。

HTML:

<choice-tree ng-model="myTree" my-alert="myAlert()"></choice-tree>

指令:

app.directive('choiceTree', function () {
    return {
        template: '<ul><choice ng-repeat="choice in tree"></choice></ul>',
        replace: true,
        //transclude: true,
        restrict: 'E',
        scope: {
            tree: '=ngModel',
            myAlert: '&'
        },
    };
});

app.directive('choice', function ($compile) {
    return {
        restrict: 'E',
        //transclude: true,
        template: '<li>' +
            '<span ng-click="myAlert()">' +
            '<input type="checkbox" ng-checked="choice.checked"> {{choice.name}} - {{name}}' +
            '</span>' +
            '</li>',
        link: function (scope, elm, attrs) {
            //Add children by $compiling and doing a new choice directive
            if (scope.choice.children.length > 0) {
                var childChoice = $compile('<choice-tree ng-model="choice.children" my-alert="myAlert()"></choice-tree>')(scope)
                elm.append(childChoice);
            }
        }
    };
});

Fiddle