13

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

Javascript

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

users_dropdown.html

<div class="btn-group pull-right" >
    <a class="btn dropdown-toggle" data-toggle="dropdown" href="">
        Select User
        <span class="caret"></span>
    </a>
    <ul class="dropdown-menu pull-right align-left" role="menu" aria-labelledby="dropdownMenu">
        <li ng-repeat = "userList in userLists"><a ng-click="getUserDetails('{{userList.SessionId}}')" style="cursor:pointer">{{userList.UserPartyName}}</a></li>
        <li ng-hide="userLists" style="cursor:pointer"><a>No Users</a></li>
    </ul>
</div>

I need to create a function getUserDetails, which should be called while clicking on a user from the list. My question is how to define this function inside the directive itself? I wrote it in the controller. But the problem is I am having several controllers. It's not a good practice to write the same function in all controllers. If I can wrote the function common for all controller, that is inside the directive, it will be good. If you have a good solution, let me know that.


Modification

I modified my directive's js as follows

app.directive('usersDropdown', function(ConfigService,$http) {
    return {
        templateUrl: 'app/views/users_dropdown.html',
        link: function($scope, element, attrs) {
            $scope.getUserDetails = function(user_id){
                console.log(user_id);
            }
        }
    };
});

For this I am getting the result in the console as {{userList.SessionId}}. Not the value for that. But when I Inspected the function is passing expected value. enter image description here

Then why the expected value is not getting inside of that function?


Solved

I solved the issue by changing my directives html tamplate. I removed ' and {{ from it as follows.

ng-click="getUserDetails(userList.SessionId)"

2 Answers 2

23

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Edited, It was meant just for better application design :D, sot that functions may not conflict.
Thanks for the clarification. But could they ever trigger a conflict if one would declare them as singletons and not bound to the scope?
When not bound to scope, you won't be able to call the function from template.
I modified my code as you mentioned. But I got one issue. Please check the modified question.
how to call somefunction() function from controller function? possible ?
3

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.

2 Comments

Thank you for your answer. I will try by creating a service. But is it possible to define functions inside directive?
You're welcome. As you suggested, you can define functions for your directive inside the directives controller function. You could do it in the linking function aswell. It depends on the actual use case, what you should go for. A directive with a private or isolate scope should be independent from the rest of your applications controllers. If you are in the need of a function which depends heavily on the rest of your application, consider an architectural refactoring

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.