简体   繁体   中英

how to call angular js factory class

I need to call scrolltop function after the form save. I write factory class for access everywhere in my application. but factory calling is not working.

this is my factory class

    .factory('scrollTop', function () {
    return {
        start: function () {
            $window.scrollTo(0, angular.element(document.getelementsbyclassname('validation-position')).offsetTop);  
        },

    };
})

i called this function in side the save function.

     $scope.saveUpdateUserDetails = function (id) {
        if (id == undefined) {
            $scope.saveUser();
            $scope.scrollTop.start();
        } else {

            $scope.updateUser(id);
            $scope.scrollTop.start();
        }
    };

but error said " Cannot read property 'start' of undefined". How i call my factory function inside the save function.

you must inject your factory or service in your controller definition like this sample of code

app.controller('MyController',['$scope','scrollTop',function($scope, scrollTop){
$scope.saveUpdateUserDetails = function (id) {
        if (id == undefined) {
            $scope.saveUser();
            scrollTop.start();
        } else {

            $scope.updateUser(id);
            scrollTop.start();
        }
    };
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM