Monday, 15 August 2011

AngularJS - accessing ng-click in custom directive -



AngularJS - accessing ng-click in custom directive -

i'm trying head around directives, can utilize template function throw out html, however, if have ng-click within template, how can access within link function?

my directive:

app.directive('directivescroll', function () { homecoming { restrict: 'ae', replace: 'true', template: '<div class="scroll-btns">' + '<div class="arrow-left" ng-click="scrollleft(sectionid)"></div>' + '<div class="arrow-right" ng-click="scrollright(sectionid)"></div>' + '</div>', link: function(scope, elem, attrs) { $scope.scrollright = function () { console.log("scrollright clicked"); }; $scope.scrollleft = function () { console.log("scrollleft clicked"); }; } }; });

as can see, have added $scope.scrollright link function, on click, nil appears in console.

if place:

$scope.scrollright = function () { console.log("scrollright clicked"); }; $scope.scrollleft = function () { console.log("scrollleft clicked"); };

in controller (and out of directive), works expected.

any help appreciated.

your link function defined this:

link: function(scope, elem, attrs) {..}

however writing functions on $scope variable:

$scope.scrollright = function () { console.log("scrollright clicked"); }; $scope.scrollleft = function () { console.log("scrollleft clicked"); };

in case $scope not injected link function (and can't injected), link simple function parameters. should alter $scope scope , should work:

scope.scrollright = function () { console.log("scrollright clicked"); }; scope.scrollleft = function () { console.log("scrollleft clicked"); };

angularjs angularjs-directive angularjs-scope angularjs-ng-click

No comments:

Post a Comment