angularjs - How do I make a directive's functions globally available? -


imagine have singleton user-interface element want provide services entire application.

the following "works", in setmessage() available anywhere in application, via $rootscope:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.17/angular.js"></script> <body ng-app="myapp"> <script> angular.module('myapp', []) .directive('mydirective',  function($rootscope) {   return {     scope : {},     template: '<div>status: {{contents}}</div>',     controller: function($scope) {       $rootscope.setmessage = function(contents) {         $scope.contents = contents;       };     }   }; }); </script> <div my-directive=""></div> <button ng-click="setmessage('clicked')">click me</button> </body> </html> 

it works, don't it. doesn't feel anglish me. have been toying idea of create service exports function set value , function it, doesn't smell better.

any suggestions?


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -