0

Hi I am struggling with the following:

http://jsfiddle.net/uqZrB/9/

HTML

<div ng-controller="MyController">
    <p>Button Clicked {{ClickCount}} Times </p>
    <my-clicker on-click="ButtonClicked($event)">
    </my-clicker>
</div>

JS

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

MyApp.directive('myClicker', function() {

    return {

            restrict: 'E',
            scope: {
                onClick: "="
            },
            link: function($scope, element, attrs) {                             

                var button = angular.element("<button>Click Me</button>");
                button.bind("mousedown", $scope.onClick);                
                element.append(button);

            }
        };

});

MyApp.controller("MyController", function ($scope) {
    $scope.ButtonClicked = function($event) {
        $scope.ClickCount++;
    };
    $scope.ClickCount = 0;
});

(using angular1.2 rc : https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js)

The custom directive "myClicker" should insert a button into the tag, and bind its mousedown event to a function supplied in the directive scope...

I.e. i can pass the a function from the controller, to execute when the button is clicked.

As you can see, when you run the fiddle, the bound event gets run 11 times, on load.... i.e. before the button has event been clicked.

Running its 11 times causes the "10 $digest() iterations reached. Aborting!" error.

Then, when I click the button I get "Cannot call method 'call' of undefined", as if the method was not declared in the scope.

  1. Why does angular try and run the method on loading?

  2. Why is the "onClick" method not available in the scope?

I think I am misunderstanding something about the directive's isolated scope.

Thanks in advance!

1
  • onClick: "=" should be onClick: "&" it's a function Commented Oct 15, 2013 at 11:48

2 Answers 2

3

The onClick: "=" in your scope definition expects a two-way data-binding, use onClick: "&" to bind executable expressions into an isolate scope. http://docs.angularjs.org/guide/directive

Sign up to request clarification or add additional context in comments.

Comments

1

please changes your code in controller as below

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

MyApp.directive('myClicker', function() {

return {

        restrict: 'E',
        scope: {
            onClick: "&"
        },
        link: function($scope, element, attrs) {                             

            var button = angular.element("<button>Click Me</button>");
            button.bind("mousedown", $scope.onClick);                
            element.append(button);

        }
    };

});

MyApp.controller("MyController", function ($scope) {
$scope.ButtonClicked = function($event) {        
    $scope.ClickCount++;
};
$scope.ClickCount = 0;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.