AngularJS - accessing ng-click in custom directive

前端 未结 2 1178
执念已碎
执念已碎 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");
        };
    

提交回复
热议问题