javascript - ng-click 不在指令内工作

标签 javascript angularjs

这是带有单击时应该调用的函数的指令。

    ebApp.directive('monthDir', function () {
    return {
        restrict: 'E',
        templateUrl: 'htmlFiles/monthDirective.html',
        transclude: true,
        scope: {
            ebObj: "=obj"
        },
        link: function link(scope, element, attrs, ngModelCtrl) {
            scope.removeDir = function (removeVal) {
                console.log("asd"); //not showing in the console

            }
            console.log(scope);
        },
        controller: function ($scope) {

        }
    }
})

以下指令中的 ng-click 不起作用。该指令的 html

    <div class="row monthDirC">
    <span class="glyphicon glyphicon-remove-sign pull-right cursorC" 
  ng-click="removeDir(ebObj.costArray[count])" ></span>

    <div class="form-group">
        <label for="datepick" class="col-md-6">Select month</label>
        <md-datepicker id="datepick" class="col-md-6" ng-model="ebObj.costArray[count].myDate"
                       md-placeholder="Enter date"
                       md-min-date="minDate"
                       md-max-date="maxDate">

        </md-datepicker>
    </div>

使用该指令的 html:

<div class="col-md-12">
        <month-dir ng-transclude ng-repeat="count in ebObj.costArray[0].countArray" obj="ebObj.costArray[count+1]"></month-dir>
</div>

最佳答案

它工作正常。确保您没有任何错误。试试这个,

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

ebApp.controller('MainCtrl', function($scope) {
  $scope.ebObj = 'someVal';
});

ebApp.directive('monthDir', function() {
    return {
        restrict: 'E',
        template: '<div ng-click="removeDir()"><b>Click Me</b><ng-transclude></ng-transclude></div>',
        transclude: true,
        scope: {
            ebObj: '=obj'
        },
        link: function link(scope, element, attrs, ngModelCtrl) {
            scope.removeDir = function (removeVal) {
                console.log('asd'); //not showing in the console
            }
        },
        controller: function ($scope) {

        }
    }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div ng-app="ebApp" ng-controller="MainCtrl">
  <month-dir ebObj="ebObj"><i>Click Me!</i></month-dir>
</div>

关于javascript - ng-click 不在指令内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45141369/

相关文章:

javascript - Node Angular View 错误

javascript - 如何向 TS 表明 store 中的值是有值(value)的,即使它是可选的?

angularjs - Angular 的 e2e 测试中的页面对象设计模式

mysql - Express Angular 和 mysql

javascript - jQuery 在多个选择器时隐藏显示

javascript - 选择内部ng表,当重新加载ng表中的数据时,页面返回顶部

angularjs - 当 http 请求正在进行时如何获取我的 Controller 的数据?

javascript - react-native 中的图像压缩

javascript - 选择选项在更改时从 ajax 生成值

javascript - 随时更改 underscore.js 模板变量