javascript - angular scope in controllers + directives -


i'm new angular , i'm having issue scope between directive , controller. here code:

controller:

var myapp = angular.module('myapp', []);      myapp.controller('testctrl', function ($scope, $http) {      $scope.dosomething = function() {        alert("testing scope");    }; }); 

directive:

myapp.directive('keyevents', function($document) {         return {             restrict: 'a',             link: function(scope, element, attrs) {                 $document.on('keypress', function(e) {                  switch (e.keycode) {                     case (49):                             dosomething();                             break                          default:                     }                  });              }            };          });  

html:

     <script src= "js/main.js"></script>      <script src = "js/keyevents.js"></script>       <body ng-app ="myapp">         <div ng-controller="testctrl">                <div key-events>                </div>         </div>       </body> 

i getting error: uncaught referenceerror: dosomething not defined - how use dosomething function inside directive?

this line:

dosomething(); 

should be

scope.dosomething(); 

because when define method in scope controller , scope inherited in directive, can access through scope passed argument.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -