Home AngularJS Calling the controller method from directive
Reply: 0

AngularJS Calling the controller method from directive

user1863
1#
user1863 Published in May 23, 2018, 4:40 pm

Prompt as from a directive to cause a method of the controller. Directive

app.directive('scroll', function($location){
    return{
        restrict: 'A',
        link: function(scope, element, attrs){
            element.on('scroll', function(){
                let fh = $('#ngview').height();
                let nh = Math.round($(element).height()  + $(element).scrollTop());
                if(fh == nh){
                    //Here we do what we need 
                }
            })
        }
    }
});

HTML markup

<div class="col-md-12 middle-body" scroll>
    <div ng-show="showUserModal" ng-include="'partial/loginModal.html'"></div>
    <div class="user-loader" ng-show="loading">
        <div class="spinner"></div>
    </div>
    <div ng-view id="ngview">
    </div>
</div>

app is the main application module

var app = angular.module('app', 
[
    'ngRoute',
    'lastUpdateModule',
    'selectedByGenreModule',
    'currentFilmModule',
    'httpFactory',
    'userModule',
    'accountModule'
]);

The controller from which you want to call the method is described in a separate file and connects via require

const SelectedByGenreModule = require('../controllers/selectedByGenre.controller.js')

and passed as a dependency to the main module

So it is from this controller that I need to call the method in the directive. Tell me how to do it correctly. I left through $rootScope but it did not work out As far as I know, the directive has the same scope as the controller in which it is located. That is, the directive is in the controller which is the parent for the controller from which you need to call the method.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314453 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO