javascript - How to call link function method from html ng-click? -
i have following code in directive:
testmodule.directive('testone',function(){ return{ replace: true, controller: 'testctrl', link: function(scope, element, attrs){ element.on('click', function(e) { //my own code there }); } } }
i wanted call above click function on ng-click below event in html(ng-click="mytest()"). how can call or write can execute function requirements in above directive's element click function per requirements.
html:
<div test-one ng-repeat="items in testjson"> <div class="title">{{items.name}}</div> <div class="context-menu"> <ul> <li ng-click="mytest()">testfunction</li> </ul> </div> </div>
thanks in advance.
first make first letter of directive simple
directive('testone',function(){
to
directive('testone',function(){
then create scope function ng-click inside link function
.directive('testone',function(){ return{ restrict : 'a', replace: true, link: function(scope, element, attrs){ scope.mytest = function() { console.log("working") } } } })
demo
angular.module("app",[]) .controller("ctrl",function($scope){ $scope.testjson = [{"name":"sss"},{"name":"ssss"}] }).directive('testone',function(){ return{ restrict : 'a', replace: true, link: function(scope, element, attrs){ scope.mytest = function() { console.log("working") } } } })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <div test-one ng-repeat="items in testjson"> <div class="title">{{items.name}}</div> <div class="context-menu"> <ul> <li ng-click="mytest()">testfunction</li> </ul> </div> </div> </div>
Comments
Post a Comment