简体   繁体   English

ng-单击角度指令

[英]ng-click in angular directive

I cannot figure out why my ng-click inside of my directive will not fire the fooControls clickTest. 我无法弄清楚为什么我的指令内的ng-click不会触发fooControls clickTest。 Why is clickTest not firing and logging to the console? 为什么clickTest无法启动并登录到控制台? 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 的Controler

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

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

HTML 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. 上面的html是foo-list的复制粘贴。 The full html is 完整的html是

<html>
<head>
</head>
<body>
<foo-list  obj="searchResults"></foo-list>
</body>
<html

Your html should be changed to have the ClickTest function applied directly to the scope, not a variable in the scope. 您的html应该更改为将ClickTest函数直接应用于范围,而不是范围中的变量。 You also need to include a <foo-list /> tag for your directive. 您还需要在指令中包含<foo-list />标记。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM