Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to call controller function from directive after update my records below code which is used

Controller.js

    app.controller('GetAlphabetical', function ($scope, $http) {
     function getCutomers() {

            $scope.loading = true;
            $http.get(ROOT + "Home/GetPesrons").then(function (response) {
                //var _data = angular.fromJson(response);
                $scope.loading = false;
                $scope.Customer = response.data; // please check the request response if list id in data object 
            }, function (error) {
                throw error;
            })
        }

        getCutomers();



app.directive('popOver', function ($compile, $http) {

    var itemsTemplate = "<ul class='unstyled'><input type='text' ng-model='items.ProfaneWord' class='form-control'><button id='popeditSave' class='btn-block btn btn-    danger' ng-click='popeditSave(items.ProfaneWord,items.pID, items.LanguageID)'>Save</button>";
    var getTemplate = function (contentType) {
        var template = '';
        switch (contentType) {
            case 'items':
                template = itemsTemplate;
                break;
        }
        return template;
    }
    return {
        restrict: "A",
        transclude: true,
        template: "<span ng-transclude></span>",
        link: function (scope, element, attrs) {
            var popOverContent;
            if (scope.items) {
                var html = getTemplate("items");
                popOverContent = $compile(html)(scope);
            }
            scope.popeditSave = function (ProfaneWord, pID, LanguageID) {
                var dataToPost = { proPhen: ProfaneWord, ID: pID, LangID: LanguageID };
                $http.post('/Home/UpdateLibrary', dataToPost)
                    .success(function (serverResponse, status, headers, config) {


                        getCutomers();
                    }).error(function (serverResponse, status, headers, config) {
                        $("#succmsg").html("Update failure..").show(500).delay(5000).fadeOut(1000);
                    }
                );
            }




            var options = {
                content: popOverContent,
                placement: "bottom",
                html: true,
                title: scope.title
            };
            $(element).popover(options);
        },
        scope: {
            items: '=',
            title: '@'
        }
    };
});

});

I want to call getCustomer() function after edit my records.

share|improve this question
1  
move those data retrieving functions to a factory or service, and inject them wherever you like. – Jorg Sep 7 '15 at 6:16
1  
fomat your code in your profile :) – Raghavendra Sep 7 '15 at 6:16

change

function getCutomers() {}

to

$scope.getCutomers = function(){}

and in directive call

scope.getCutomers();
share|improve this answer
    
Its not working friends – Nayeem Mansoori Sep 7 '15 at 6:27
    
can we see your html..? – maddygoround Sep 7 '15 at 6:27
    
While this probably is a perfectly good answer, please consider explaining why and how it works. – Gerald Versluis Sep 7 '15 at 7:18

I have created a directive that check the username is unique or not

app.directive('ngUnique', ['$http', function ($http) {
    return {
        require: 'ngModel',
        link: function (scope, elem, attrs, ctrl) {
            elem.on('blur', function (evt) {
                scope.$apply(function () {
                    $http({
                        method: 'POST',
                        url: '/User/IsUserExist',
                        data: {
                            username: elem.val(),
                            dbField: attrs.ngUnique
                        }
                    }).success(function (data, status, headers, config) {
                        if (attrs.ngUnique == "username")
                            ctrl.$setValidity('unique', data);
                        else if (attrs.ngUnique == "email")
                            ctrl.$setValidity('uniqueEmail', data);
                        else if (attrs.ngUnique == "oldpassword")
                            ctrl.$setValidity('unique', data);
                        return undefined;
                    });
                });
            });
        }
    }
}]);

please create your directive like above code this will work

in my project its working fine to check username and email unique

share|improve this answer

Add this in your directive

scope: {
    items: '=',
    title: '@',
    getCustomer:'&getCustomer',    
}

$scope.getCustomer()(any param from directive); //call function from controller

call this from your getCustomer Html

<pop-over get-Customer="getCutomers">

and change controller to:

app.controller('GetAlphabetical', function ($scope, $http) {
    $scope.getCutomers = function() {
        $scope.loading = true;
        $http.get(ROOT + "Home/GetPesrons").then(function (response) {
            //var _data = angular.fromJson(response);
            $scope.loading = false;
            $scope.Customer = response.data; // please check the request response if list id in data object 
        }, function (error) {
            throw error;
        });
    };

    $scope.getCutomers();
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.