0

Using angular, I want to save the value of an input to a cookie using directive when clicking on save button. Here is my code, I wasn't sure if I should use link function or controller directive, but anyway $cookie is of course unknown in the click event handler. How can I pass $cookie or $log to the on Click function?

.directive('ndUserSettingCookie', ['$cookies', '$log',function() {
return {
    link: function(scope, el, attrs) {
        scope.savedMsg = ' items by page';
        var setting = [];
        scope.itemsByPage = 18;
        scope.itemsByPage = (typeof setting.itemsByPage !== "undefined")? setting.itemsByPage :scope.itemsByPage;

        el.on('click', function() {                     

            var now = new Date(),
            // this will set the expiration to 12 months
            expireDate = new Date(now.getFullYear()+1, now.getMonth(), now.getDate());

            $cookies.putObject('setting.itemsByPage', scope.itemsByPage, {'expires': expireDate});
            scope.savedMsg = scope.itemsByPage + " items per page saved to your settings!";

            scope.$apply();
        })
    }
}
}])

Please let me know if you think I should do this totally in another way.

1 Answer 1

0

sorry guys, I just have forgotten to add them to the function parameters! My Bad!

This is how it works:

.directive('ndUserSettingCookie', ['$cookies', '$log',function($cookies, $log) {
Sign up to request clarification or add additional context in comments.

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.