1

Controller Code

'use strict';

angular.module('MyApp').controller('ArticleContribEmailController', [

    '$scope', 'ArticleAppState', 'fbsUserDataService', 'contribEmailService',
    function ($scope, ArticleAppState, fbsUserDataService, contribEmailService ) {

        this.userChanged = function () {

            if (fbsUserDataService.initialized && fbsUserDataService.user && ArticleAppState.page_data) {

                // user has authenticated.
                contribEmailService.initForm();

            }

        };


        // watch for when user data is available, run userChanged.
        $scope.$watch(function() { return fbsUserDataService.user; }, this.userChanged);
        $scope.$watch(function() { return fbsUserDataService.initialized; }, this.userChanged);
    }
]);

Service Code

'use strict';

angular.module('forbesArticleApp').service('contribEmailService', [

    '$injector', '$route', 'ArticleAppState', 'fbsUserFormFactory', 'fbsUserDataService',

    function initForm ($injector, $route, ArticleAppState, fbsUserFormFactory, fbsUserDataService) {

        console.log("Hello world!");

    }

]);

I only want to fire the contribEmailService.initForm() function from the call in my controller, but it is firing as soon as the page loads.

How do I set when the service function initForm() is called?

2 Answers 2

1

Here is the corrected service code:

'use strict';

angular.module('forbesArticleApp').service('contribEmailService', [
    '$injector', '$route', 'ArticleAppState', 'fbsUserFormFactory', 'fbsUserDataService',

function($injector, $route, ArticleAppState, fbsUserFormFactory, fbsUserDataService) {
    return {
      initForm: function() {
        console.log("Hello world!");
      }
    };
]);

The service function is a factory that will in turn return the actual service. So it will run the first time it is requested as a dependency. The way you had it written, in fact, contribEmailService would have been undefined within your function, because your factory didn't actually return anything.

Hope this helps!

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

Comments

0
controller:-
blogcontroller is controller name

app.controller('blogController', function($scope, $compile,  $http, blogAuth, AppInfo, $location,$element){

$scope.blog_abuse = function(blog_id)
    {

        blogAuth.BlogAbuse(blog_id).then(function(response)
        { 
            $scope.DetailblogList.is_abused = response.records.is_abused;
        },function(error){
        });
    }
});


service:-

app.factory('AppInfo', function(){
    return {
        serviceURL:site_url
    };
});

app.service('blogAuth', function($http, $rootScope, $q, AppInfo){

this.BlogAbuse = function(blog_id){
    var deferred = $q.defer();
    var pageObj ={"blog_id":blog_id};   

        $http.post(AppInfo.serviceURL+'blog/blog_abuse',pageObj).success(function(data){
            deferred.resolve(data);
        }).error(function(msg, code) {        
            console.log('error', code, msg );
        });
        return deferred.promise;
    }

});

Comments

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.