How To Define A Function Inside Angular-js Directive
I created a directive for selecting users using bootstrap's drop-down element. as follows. Javascript app.directive('usersDropdown', function(ConfigService,$http) { return {
Solution 1:
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.
Solution 2:
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.
Post a Comment for "How To Define A Function Inside Angular-js Directive"