簡體   English   中英

ng-click在指令中不起作用

[英]ng-click not working within directive

這是帶有應在單擊時調用的函數的指令。

    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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM