[英]ng-click not working within directive
This is the directive with the function that is supposed to be called when clicked. 这是带有应在单击时调用的函数的指令。
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) {
}
}
})
The ng-click in the following directive is not working. 下列指令中的ng-click不起作用。 The directive's html
指令的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>
The html that uses the directive: 使用指令的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>
It is working correctly. 它工作正常。 Make sure you don't have any errors.
确保您没有任何错误。 Try this,
尝试这个,
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.