Javascript ng单击“不在指令范围内工作”
这是带有函数的指令,该函数应在单击时调用Javascript ng单击“不在指令范围内工作”,javascript,angularjs,Javascript,Angularjs,这是带有函数的指令,该函数应在单击时调用 ebApp.directive('monthDir', function () { return { restrict: 'E', templateUrl: 'htmlFiles/monthDirective.html', transclude: true, scope: { ebObj: "=obj" }, link:
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',函数$scope{ $scope.ebObj='someVal'; }; ebApp.directive'monthDir',函数{ 返回{ 限制:'E', 模板:“单击我”, 是的, 范围:{ ebObj:'=obj' }, 链接:函数linkscope、元素、属性、ngModelCtrl{ scope.removeDir=函数removeVal{ console.log'asd';//不显示在控制台中 } }, 控制器:函数$scope{ } } } 点击我!
尝试在以下位置删除函数名链接:函数链接范围、元素、属性、ngModelCtrl{->链接:函数范围、元素、属性、ngModelCtrl{@WasifKhan.试过了。不起作用。我的模板中的指令可能有什么问题。当我使用模板URL时,它不起作用。我在指令的输入字段上添加了$watch。但没有起作用。我编辑了答案中的代码。试着从html中删除ng transclude并将其放在模板中。我编辑答案是为了更好理解。通常我们使用ng transclude绑定指令中的innerHTML值。您可以参考更多。