1

I cannot figure out why my ng-click inside of my directive will not fire the fooControls clickTest. Why is clickTest not firing and logging to the console? is there a better way of doing this?

Directive

app.directive('fooList', function () {
    return {
        restrict: 'E',
        templateUrl: './app/views/fooList.html',
        scope: { obj: "=" },
        controller: 'fooController',
        controllerAs: 'b'
    };
});

Controler

app.controller('fooController', ['$scope', function ($scope) {
    $scope.obj = [];

    $scope.ClickTest = function (num) {console.log(num);};
}]);

HTML

<div ng-repeat="book in obj" class="container">
    <div class="row">
        <h4 class="pull-right"><button class="btn btn-small" ng-click="b.ClickTest(1)">ClickTest</button></h4>
    </div>
    <br />
</div>

EDIT

The above html is a copy paste of foo-list. The full html is

<html>
<head>
</head>
<body>
<foo-list  obj="searchResults"></foo-list>
</body>
<html
7
  • 1
    In your example code the <foo-list> tag is not even used... Commented Mar 25, 2015 at 14:43
  • 1
    Also, why is it b.ClickTest(1). You have ClickTest applied to your scope, it should just be ClickTest(1) Commented Mar 25, 2015 at 14:44
  • thank you @mcpDesigns it was the alias that was messing it up. I have this working in another project and the alias was just fine. Anyway removing the alias cleared up the issue. Thank you Commented Mar 25, 2015 at 14:46
  • If you make that the answer I will mark it as such. Commented Mar 25, 2015 at 14:47
  • 1
    No worries! Glad to of helped either way Commented Mar 25, 2015 at 15:09

1 Answer 1

1

Your html should be changed to have the ClickTest function applied directly to the scope, not a variable in the scope. You also need to include a <foo-list /> tag for your directive.

<div ng-repeat="book in obj" class="container">
    <div class="row">
        <!-- Change b.ClickTest(1) to ClickTest(1)-->
        <h4 class="pull-right"><button class="btn btn-small" ng-click="ClickTest(1)">ClickTest</button></h4>
    </div>
    <br />
    <!-- Insert a foo-list tag -->
    <foo-list  obj="searchResults"></foo-list>
</div>
Sign up to request clarification or add additional context in comments.

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.