AngularJS - accessing ng-click in custom directive

前端 未结 2 1179
执念已碎
执念已碎 2020-12-17 16:09

I\'m trying to get my head around directives, i can easily use the template function to throw out my HTML, however, if i have an ng-click within my template, how can i acces

相关标签:
2条回答
  • 2020-12-17 16:38

    Your link function is defined like this:

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

    however you are writing functions on $scope variable:

        $scope.scrollRight  = function () {
          console.log("scrollRight  clicked");
        };
        $scope.scrollLeft  = function () {
          console.log("scrollLeft  clicked");
        };
    

    In this case $scope is not actually injected into link function (and can't be injected), so link is just simple function with parameters. You should change $scope to scope and it should work:

        scope.scrollRight  = function () {
          console.log("scrollRight  clicked");
        };
        scope.scrollLeft  = function () {
          console.log("scrollLeft  clicked");
        };
    
    0 讨论(0)
  • 2020-12-17 17:03

    Are you sure the link function parameter is named scope, not $scope?

    0 讨论(0)
提交回复
热议问题