Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:58:41+00:00 2026-06-18T01:58:41+00:00

please see my fiddle and help me find a way to access function myAlert

  • 0

please see my fiddle and help me find a way to access function myAlert from nested directive “choice”. A found some solutions where was shared some properties of scope like this: http://jsfiddle.net/zbD95/6/ but I need use functions and properties from scope together.

Thanks!!!

Here is duplicate of my fiddle:
HTML part of fiddle:

<!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 part of fiddle:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:58:42+00:00Added an answer on June 18, 2026 at 1:58 am

    Use the ‘&’ syntax to allow your isolated scope to call a method defined on the parent scope. Also, you don’t need any transclusion for these directives.

    HTML:

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

    Directives:

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see my this fiddle: jsfiddle.net/jWKdK/ And help me to add border to the
Please see the fiddle ( http://jsfiddle.net/Jk6Bk/5/ ) I would like to add some spacing
Please see this fiddle . For me, it's just a self-executing empty function: function(){}()
Please see this fiddle I want the scrolling div stop following when left div
can you please help me to find out if it takes longer for a
Please see this fiddle. In Chrome (version 23) and Safari , if you keep
First: please see this fiddle: http://jsfiddle.net/gamehelp16/77ssr/ So, these images: (source: placekitten.com ) Uses the
Please see this fiddle for my situation. Essentially, I have a floating span which
Please see this fiddle (the result window should be made large). When one of
Please see this Fiddle After I upgraded to FF 16.0.2, the absolute position has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.