ng-click不在指令内工作

时间:2017-07-17 10:06:55

标签: 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>

1 个答案:

答案 0 :(得分:1)

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

&#13;
&#13;
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) {

        }
    }
})
&#13;
<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>
&#13;
&#13;
&#13;