How to define a function inside Angular-js Directive

后端 未结 2 1727
礼貌的吻别
礼貌的吻别 2020-12-30 05:45

I created a directive for selecting users using bootstrap\'s drop-down element. as follows.

Javascript

app.directive(\'usersDropdown         


        
相关标签:
2条回答
  • 2020-12-30 06:27

    If you find yourself in the need of a function or data variable in various places along your application, it could possibly be a good solution to move some of your logic into a service.

    You could, for example, create a user service, which should provide application-wide access to user-related information and methods.

    0 讨论(0)
  • 2020-12-30 06:40

    The link Directive function is a js function, inside which you may do anything

    app.directive('usersDropdown', function(ConfigService,$http) {
        return {
            scope : {},
            templateUrl: 'app/views/users_dropdown.html',
            link: function($scope, element, attrs) {
                $scope.somefunction = function(a) {
                    // Do some stuff
                }
            }
         }; 
    });
    

    Also you should use isolated scope to accomplish, if you want this function not be accesible from outside the directive.

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